mst*_*ing 4 signature typescript typescript-typings
使用时,MyInterfaceOne[] | MyInterfaceTwo[]在代码的其他位置出现Typescript错误,这对我来说没有意义。
myInterfaces = funcWithAboveSignature()
return myInterfaces.reduce(async (acc, interfaceType) => {
await acc;
if (interfaceType.prop) {
return await someAsyncFunc(interfaceType);
} else {
return await someOtherAsyncFunc(interfaceType);
}
}, Promise.resolve());
Run Code Online (Sandbox Code Playgroud)
错误提示:
Argument of type '(acc: MyInterfaceOne, raw: MyInterfaceOne) => Promise<void>' is not assignable to parameter of type '(previousValue: MyInterfaceOne, currentValue: MyInterfaceOne, currentIndex: number, array: MyInterfaceOne[]) => MyInterfaceOne'.
但是,当将功能签名从更改为时MyInterfaceOne[] | MyInterfaceTwo[],(MyInterfaceOne|MyInterfaceTwo)[]它可以工作。
有人可以向我解释这两者之间的区别吗?
小智 5
MyInterfaceOne[] | MyInterfaceTwo[]
Run Code Online (Sandbox Code Playgroud)
说它可以是仅MyInterfaceOne的数组,也可以是仅MyInterfaceTwo的数组。
(MyInterfaceOne|MyInterfaceTwo)[]
Run Code Online (Sandbox Code Playgroud)
说它可以是MyInterfaceOne或MyInterfaceTwo的数组,因此它可以包含两种类型中任何一种的元素。