gpt4 book ai didi

javascript - 如何在React Native中全局重新定义FormData TypeScript接口(interface)?

转载 作者:行者123 更新时间:2023-12-02 22:09:05 28 4
gpt4 key购买 nike

当像这样在 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 提示...

Argument of type '{ uri: any; name: string; type: string; }' is not assignable to parameter of type 'string | Blob'. Object literal may only specify known properties, and 'uri' does not exist in type 'Blob'.ts(2345)


所以我能够像这样重新定义 FormData...( hat tip )

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>;
}


这直接在我正在执行append的文件中效果很好。

如何在项目中全局定义它,以便它覆盖 FormData 的所有使用?

最佳答案

使用全局ambient module扩展 lib.dom.d.ts 中定义的 FormData 接口(interface)通过interface merging :

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;
}
}

关于javascript - 如何在React Native中全局重新定义FormData TypeScript接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59617587/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com