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:
true|false boolean controlled flow
boolean type narrow
Code
functionabsurd(_: never){thrownewError("Must not be called");}interfaceFoo{bar: false;}functionfooled(foo: Foo){if(foo.bar){// foo expected to be narrowed to 'never' typeabsurd(foo);}else{constbar: false=foo.bar;}}
Expected behavior:
foo type in the 'true' branch narrowed to never
Actual behavior:
foo type is Foo, which is nonsense
Foo is not a union, it's just a single object type. Therefore you don't get discriminated union behavior, and TS won't narrow arbitrary objects based on their properties. It works the way you expect if you use a proper discriminated union:
functionabsurd(_: never){thrownewError("Must not be called");}interfaceFoo{bar: false;}interfaceBar{bar: true;}functionfooled(foo: Foo|Bar){if(foo.bar){}elseif(!foo.bar){}else{absurd(foo);}}
TypeScript Version: 3.9.2, Nightly
Search Terms:
true|false boolean controlled flow
boolean type narrow
Code
Expected behavior:
foo type in the 'true' branch narrowed to never
Actual behavior:
foo type is Foo, which is nonsense
Playground Link: https://www.typescriptlang.org/play/?ts=4.0.0-dev.20200615#code/GYVwdgxgLglg9mABAQwEYGcQCcAmAKAfQC5EwBTANzKwEpEBvRKACyzgHdSzOBRLNrHgBEAWRDoopOJNRlEEZABtFZHEJoBuRAF8AULphgo1YMghyAYnDgNdie4lTIsJU4vRkNuvbtCRYCIjA1ir4wXAkVnB09HYOMMCIeOEAdE60tg5ZiAD0OUHWiGQAHgAOZNCqTDaypM5s7FVQNgDk5FRYLUwAnuVx2SgY2GHWmv06Re5ysQP2EAgSjs6uSh6IALwFcGnOWuN6ekA
Related Issues:
No
The text was updated successfully, but these errors were encountered: