如果我输入以下内容:
interface A {
x: number
}
interface B {
y: number
}
type Z = A | B;
// here it allows me to create a variable of type Z with both members of type A and B.
let z: Z = {
x: 5,
y: 6,
}
我无法确保类型 Z 的对象确实包含 A 的所有成员但不包含 B 的成员(或相反)。 TypeScript 有可能吗?经过大量研究后,我倾向于“否”的答案,但我不确定。
最佳答案
默认情况下,联合将提供的所有选项合并为一个,但您有两个选项。你想要的东西在 TypeScript 中不可用,但在 TS 项目的列表中有类似的问题(此时状态:“打开”)以及两个很好的解决方法。
一个选项不可用:目前 TypeScript 中没有确切的类型 (unlike in Flow)。有TS问题#12936 "Exact Types"那现在仍然是开放的。
在 TS 的未决问题列表中还有另一个问题/提案确切地询问了您的问题:#14094 "Proposal: Allow exclusive unions using logical or (^) operator between types"
您可以使用这些解决方法:
选项#1
在 TypeScript 和 Flow 中,您可以使用标记类型来创建 XOR 联合而不是 OR 联合。
interface A {
kind: 'A',
x: number
}
interface B {
kind: 'B',
y: number
}
type Z = A | B;
// here it DOES NOT allow to create a variable of type Z with both members of type A and B.
let z: Z = {
kind: 'A',
x: 5
// y: 6 will produce an error
}
选项 #2
第二个选项是设置所有类型的所有属性,但将那些不应该存在的属性设置为undefined
:
interface A {
x: number,
y?: undefined
}
interface B {
x?: undefined,
y: number
}
type Z = A | B;
let z: Z = {
y: 5
// y: 6 will produce an error
}
作为记录,在 Facebook 的类型系统中 Flow您可以通过使用 disjoint union (XOR) 而不是仅使用 union (OR) 或使用确切的对象类型或对于上述 TS 将不需要的属性设置为 undefined
。 I made a Flow demo with your example (link) .在这种情况下,Flow 的代码与 TS 的代码相同。
关于typescript - 在 typescript 中提供一种对象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47315346/