在
test
函数内部,当 b
类型为 string
时,a
类型不会缩小为 string
。type Foo = {
a: string;
b: string;
};
type Bar = {
a: number;
b: number;
};
type Uinon = Foo | Bar;
function test({ a, b }: Uinon) {
if (typeof a === "string") {
a;
b;
}
}
如果 a 是如下所示的类型文字,这很好,但是如果它不是类型文字,为什么它不是类型缩小?
type Foo = {
a: "A";
b: string;
};
type Bar = {
a: "B";
b: number;
};