A
当我在两个对象类型(和)之间创建联合时B
,我希望分配给该联合类型的变量只能采用A
或的形状B
。
出乎意料的是,bad
这里没有类型错误,尽管直观上应该如此,因为它不满足A
nor的约束B
。Typescript Playground 链接
type A = {a: string}
type B = {a: string, b: string, c: string}
type Foo = A | B
// this should be a type error, but it isn't?
// to be valid, it should either:
// - not have "c" property
// - add the "b" property
const bad: Foo = {
a: "",
c: ""
}
我该如何改进Foo
才能使bad
变量因类型错误而失败?