【发布时间】:2021-05-26 17:59:33
【问题描述】:
我有一个函数可以接收两个不同的对象签名作为参数。我该如何输入?
即
fn({ a: 'a', c: 'c' })
fn({ b: 2, c: 'c' })
但是我尝试定义参数类型,TS 抱怨该类型不存在 a 和 b(只有两者都存在的 c 很好)
export type IUploadImageArgs =
| {
uri: string;
type: string;
path: string;
}
| {
path: string;
file: any;
};
const uploadImage = async ({ file, path }: IUploadImageArgs) => {
}
// Property 'file' does not exist on type 'IUploadImageArgs'
旁注: 这是一个react native + web项目,一个抽象文件上传的函数,其中web版本接受文件本身,而native版本接受文件的本地路径。
谢谢!
【问题讨论】:
-
function fn(args:{a:string, c:string}|{b:number, c:string})怎么样? -
我试过了,我得到错误 a/b 在 args 上不存在(编辑问题以反映这一点)
标签: typescript