代码如下:
export type Writable<T> = {
-readonly [K in keyof T]: T[K];
};
class Foo {
readonly foo = 'foo';
}
class Bar {
readonly bar = 'bar';
}
type WritableFooBar = Writable<Foo | Bar>;
declare function isFoo(obj: any): obj is Foo;
function test(foobar: WritableFooBar): void {
if (!isFoo(foobar)) {} // happens also with if(isFoo())
foobar
// ^? Foo | Writable<Bar>
}
在这种情况下,类型推断是被破坏了,还是我遗漏了什么?
Playground
1条答案
按热度按时间x9ybnkn61#
这实际上是4.8中出现的回归,在5.0中得到了修复。