You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search Terms:
discriminated union
union
type predicate
type guard
narrow
Code
interfaceA{kind: "A";foo?: string;}interfaceAAextendsA{foo: string;}interfaceB{kind: "B";foo: string;}interfaceBBextendsB{//blah: string; // comment or uncomment this line}functionisDefiniteFoo(x: A|B) : x is AA|BB{return!!x.foo;}letx!: A|B;if(isDefiniteFoo(x)){// Without line 16, the type of x is narrowed to B// If line 16 is present, the type of x is narrowed to AA | BBletz=x;// Note that BB isn't required to expose the bug. If the return type on// isDefiniteFoo is : x is AA | B, x is still narrowed to B rather than AA | B}
Expected behavior:
The type of x should be narrowed to AA | BB.
A is not assignable to AA | BB therefore it gets filtered out by the type predicate. B is assignable to AA | BB (without line 16) and is therefore retained by the type predicate.
With line 16, neither are related so it falls back to full check where AA | BB is a subtype of A | B, so that is the result of the predicate.
TypeScript Version: 4.0.0-dev.20200803
Search Terms:
discriminated union
union
type predicate
type guard
narrow
Code
Expected behavior:
The type of
x
should be narrowed toAA | BB
.Actual behavior:
The type of
x
is narrowed toB
.Playground Link:
https://www.typescriptlang.org/play?ts=4.0.0-dev.20200803#code/JYOwLgpgTgZghgYwgAgILIN4Chm+Qa1ABMAuZAIlXIG4c8YB7BgfjIGcwpQBzWgXyxZQkWIhSp0EAB6QQRNmkx1cjBu049+g4dHhJkAISV4CxMuQM1lyVeq4heWAUPC6xho9NnzDxvAHp-ACMAGzgACztNZEDkBAYAWwSIcGQGKGQAVxB4pJSwZDBw4AUQ0AgnQRhshDBgBhBkEoARCBhQYEgAMSYACikydAAfQwBKZDIpJoUJZBGDI2wTKAgwTKhGgEJNqQA6VS1BENXkKU3BucNaYBhkXpa2ju6+qVHxpYD-ZAB1TvCGTIFMogFAARgAbAAaQrhFBgACeAAcUAxblMSsgQHAoFAGAB3CBEQoMQzWWIASVuwLB4OmyERKzY+WhRThSJRaLpWJx+MJxLQww81mOBQAXsgALynWhkr4AOQYkBhcAKC2mIAA5AUVgBHTLAFZEsAk6SIhhMmEoIKZbi7ZCUy3IFZrDaFdlpECy6atdogToQHokjGTOmzebQ9EKDjAEIhTHY3EEo0koxQFWwjJFOCNMOkvhAA
The text was updated successfully, but these errors were encountered: