[英]How to globally redefine the FormData TypeScript interface in React Native?
When doing a simple append
with FormData in React Native like this...当像这样在 React Native 中用 FormData 做一个简单的
append
时......
const formData = new FormData();
formData.append('image-data', {
uri: '/awesome-chat-app/ImagePicker/abb9-a435046e971c.jpg',
name: 'image001.jpg',
type: 'image/jpeg',
});
TypeScript complains...打字稿抱怨...
Argument of type '{ uri: any;
'{ uri: any; 类型的参数name: string;
名称:字符串; type: string;
类型:字符串; }' is not assignable to parameter of type 'string |
}' 不能分配给 'string | 类型的参数Blob'.
斑点'。 Object literal may only specify known properties, and 'uri' does not exist in type 'Blob'.ts(2345)
对象字面量只能指定已知属性,而 'uri' 不存在于类型 'Blob'.ts(2345)
So I was able to redefine FormData like this... ( hat tip )所以我能够像这样重新定义 FormData ......( 帽子提示)
interface FormDataValue {
uri: string;
name: string;
type: string;
}
interface FormData {
append(name: string, value: string | Blob | FormDataValue, fileName?: string): void;
delete(name: string): void;
get(name: string): FormDataEntryValue | null;
getAll(name: string): FormDataEntryValue[];
has(name: string): boolean;
set(name: string, value: string | Blob | FormDataValue, fileName?: string): void;
}
declare let FormData: {
prototype: FormData;
new (form?: HTMLFormElement): FormData;
};
interface FormData {
entries(): IterableIterator<[string, string | File]>;
keys(): IterableIterator<string>;
values(): IterableIterator<string | File>;
[Symbol.iterator](): IterableIterator<string | File>;
}
This works great directly within the file I'm doing the append
.这直接在我正在执行
append
的文件中工作得很好。
How do I define it globally in the project so it overrides all uses of FormData?我如何在项目中全局定义它以便它覆盖 FormData 的所有使用?
Use a global ambient module to extend the FormData
interface defined in lib.dom.d.ts
via interface merging :使用全局环境模块通过接口合并扩展
lib.dom.d.ts
定义的FormData
接口:
declare global {
interface FormDataValue {
uri: string;
name: string;
type: string;
}
interface FormData {
append(name: string, value: FormDataValue, fileName?: string): void;
set(name: string, value: FormDataValue, fileName?: string): void;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.