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
{{ message }}
Repository navigation
Discriminated Unions Incompatible with Intersection Types #11989
TypeScript Version: 2.0.3 AND nightly (2.1.0-dev.20161102)
Code
interfaceFoo{kind: 'foo';a: number;}interfaceBar{kind: 'bar';b: string;}functiontest(x: Bar|Foo){if(x.kind==='foo'){console.log(x.a);// Valid, as expected}}interfaceQux{c: boolean;}functiontest2(x: (Bar|Foo)&Qux){if(x.kind==='foo'){// Type of x should be Foo&Qux now, but is still (Bar|Foo)&Quxconsole.log(x.a);// Error}}
Expected behavior:
In test2(), the discriminated union check should narrow the type down to (Foo)&Qux, letting you access property a, since a is guaranteed to be there if x.type === 'foo'.
Actual behavior:
No type narrowing is done whatsoever. The type of x remains (Foo|Bar)&Qux.
This looks like a duplicate of #9919, which was fixed in #11717. I can compile your example with no error in the nightly build and x is narrowed to Foo & Qux as expected. Can you reconfirm that there's an issue in the nightly?
Anders Hejlsberg (@ahejlsberg) Ah I stand corrected. tsc on the command line does indeed work, it's just that it's still be reported as an error in VS Code. Not sure whether to report that as a different issue or not.
Also I do have another, different but related bug that is reproduceable on the command line on the nightly build :)
Activity
ahejlsberg commentedon Nov 2, 2016
This looks like a duplicate of #9919, which was fixed in #11717. I can compile your example with no error in the nightly build and
xis narrowed toFoo & Quxas expected. Can you reconfirm that there's an issue in the nightly?MikeyBurkman commentedon Nov 2, 2016
Anders Hejlsberg (@ahejlsberg) Ah I stand corrected. tsc on the command line does indeed work, it's just that it's still be reported as an error in VS Code. Not sure whether to report that as a different issue or not.
Also I do have another, different but related bug that is reproduceable on the command line on the nightly build :)