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
typea={typeFoo: "a"};typeb={typeFoo: "b"};typec={typeBar: "c"};typed={typeBar: "d",value: number};typefoo=a|b;typebar=c|d;//throws build error as value does not exist on 'a & c'constx: a&bar={typeFoo:"a",typeBar: "c",value:100}//throws build error as value does not exist on 'b & c'consty: b&bar={typeFoo:"b",typeBar: "c",value:100}//does not throw a build error, but shouldconstz: foo&bar={typeFoo:"a",typeBar: "c",value:100}
Expected behavior:
Object literal 'z' may only specify known properties, and 'value' does not exist in type '(a & c) | (b & c)'
Actual behavior:
Typescript compiles const z without catching the error
The text was updated successfully, but these errors were encountered:
Haubach
changed the title
Intersection of multiple union types does not narrow discriminated field
Intersection of multiple union types does not narrow properties
Mar 7, 2019
I think this can be filed under #13813 and #20863. I believe excess property checking on a union will not narrow the union if multiple fields discriminate to different types, which is exactly the case here with typeFoo and typeBar. As a consequence, field z is not considered excess in foo & bar because it appears in at least on part of the union.
TypeScript Version: 3.4.0-dev.20190307
Search Terms:
union type, intersection types,
Code
Expected behavior:
Object literal 'z' may only specify known properties, and 'value' does not exist in type '(a & c) | (b & c)'
Actual behavior:
Typescript compiles const z without catching the error
Playground Link: https://www.typescriptlang.org/play/#src=type%20a%20%3D%20%7BtypeFoo%3A%20%22a%22%7D%3B%0D%0Atype%20b%20%3D%20%7BtypeFoo%3A%20%22b%22%7D%3B%0D%0A%0D%0Atype%20c%20%3D%20%7BtypeBar%3A%20%22c%22%7D%3B%0D%0Atype%20d%20%3D%20%7BtypeBar%3A%20%22d%22%2C%20value%3A%20number%7D%3B%0D%0A%0D%0A%0D%0Atype%20foo%20%3D%20a%20%7C%20b%3B%20%0D%0Atype%20bar%20%3D%20c%20%7C%20d%3B%0D%0A%0D%0A%2F%2Fthrows%20build%20error%20as%20value%20does%20not%20exist%20on%20'a%20%26%20c'%0D%0Aconst%20x%3A%20a%20%26%20bar%20%3D%20%7B%0D%0A%20%20%20%20typeFoo%3A%22a%22%2C%0D%0A%20%20%20%20typeBar%3A%20%22c%22%2C%0D%0A%20%20%20%20value%3A100%0D%0A%7D%0D%0A%0D%0A%2F%2Fthrows%20build%20error%20as%20value%20does%20not%20exist%20on%20'b%20%26%20c'%0D%0Aconst%20y%3A%20b%20%26%20bar%20%3D%20%7B%0D%0A%20%20%20%20typeFoo%3A%22b%22%2C%0D%0A%20%20%20%20typeBar%3A%20%22c%22%2C%0D%0A%20%20%20%20value%3A100%0D%0A%7D%0D%0A%0D%0A%2F%2Fdoes%20not%20throw%20a%20build%20error%2C%20but%20should%0D%0Aconst%20z%3A%20foo%20%26%20bar%20%3D%20%7B%0D%0A%20%20%20%20typeFoo%3A%22a%22%2C%0D%0A%20%20%20%20typeBar%3A%20%22c%22%2C%0D%0A%20%20%20%20value%3A100%0D%0A%7D%0D%0A
Related Issues: #11989
The text was updated successfully, but these errors were encountered: