Typescript 结构类型解决方案不';行不通
我有一个函数,可以对不同类型的数组执行一些操作。函数可以接收的数组具有一个公共属性(即选定的Typescript 结构类型解决方案不';行不通,typescript,Typescript,我有一个函数,可以对不同类型的数组执行一些操作。函数可以接收的数组具有一个公共属性(即选定的)。我知道Typescript使用结构化键入,因此我不能像这样键入: interface typeA { propOne: string selected: boolean } interface typeB { propTwo: string propThree: string selected: boolean } function sampleFn():
)。我知道Typescript使用结构化键入,因此我不能像这样键入:
interface typeA {
propOne: string
selected: boolean
}
interface typeB {
propTwo: string
propThree: string
selected: boolean
}
function sampleFn(): void {
allSelected = someArray.every((item: typeA | typeB) => item.selected);
}
我查看了一个类似的SO问题(),并将其更改为:
function sampleFn(): void {
type select = {selected: boolean};
arr = someArray.every((item: select) => item.selected);
}
我仍然收到一个
无法调用其类型缺少调用签名的表达式的错误。我遗漏了什么?请给出一个-您的示例失败,因为未定义arr
、allSelected
和someArray
。如果我正确阅读了另一个问题,您很可能需要使用someArray
(typeA | typeB)[/code>而不是typeA[];typeB[/code>