type a = {
a: number
b: {d: string}[]
}
type b = {
c : string
b: {f: number}[]
}
type l = a & b
const ab: l = {a: 1, c: '', b: [{d: '', f:1}]}
const ac: l['b'] = [{ d: '', f: 1}]
Playground
在将类型a
和b
合并为类型l
之后,为什么类型l
的const ab
工作,而类型l['b']
的const ac
不工作?
我尝试在vscode上编写此代码,但在const ac上出现此错误:
Type '{ d: string; f: number; }[]' is not assignable to type '{ d: string; }[] & { f: number; }[]'.
Types of property 'pop' are incompatible.
Type '() => { d: string; f: number; }' is not assignable to type '(() => { d: string; }) & (() => { f: number; })'.
Type '() => { d: string; f: number; }' is not assignable to type '() => { d: string; }'.
Type '{ d: string; f: number; }' is not assignable to type '{ d: string; }'.
Object literal may only specify known properties, and 'f' does not exist in type '{ d: string; }'.
为什么类型l['b']
不包含来自类型{ d: string; }[] & { f: number; }[]
的合并类型{d: string, f: number}[]
,而它包含类型l
?
1条答案
按热度按时间e4eetjau1#
这是TypeScript中的一个已知错误,在microsoft/TypeScript#42715中报告。显然,类型为intersectedarray类型的对象属性错误地接收到多余的属性警告。在microsoft/TypeScript#43707中曾尝试修复它,但它显然在现实世界的代码库中造成了不希望的错误,因此根据此注解恢复了它。看起来它不会很快得到修复。
请注意,交叉数组会以其他不希望的方式运行,例如microsoft/TypeScript#41874,因此最好尽可能避免此类类型;你可以编写自己的实用程序类型来执行递归的类似于交集的操作,这种操作在数组中表现得更好。例如:
详细讨论该类型如何工作可能超出了问题的范围,但关键是,结果类型包含您实际需要的
{d: string, f: number}[]
类型,然后您完全避免了遇到的bug:Playground代码链接