Closed
Description
TypeScript Version: 3.4.0-dev.20190307
Search Terms:
union type, intersection types,
Code
type a = {typeFoo: "a"};
type b = {typeFoo: "b"};
type c = {typeBar: "c"};
type d = {typeBar: "d", value: number};
type foo = a | b;
type bar = c | d;
//throws build error as value does not exist on 'a & c'
const x: a & bar = {
typeFoo:"a",
typeBar: "c",
value:100
}
//throws build error as value does not exist on 'b & c'
const y: b & bar = {
typeFoo:"b",
typeBar: "c",
value:100
}
//does not throw a build error, but should
const z: 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
Related Issues: #11989
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
[-]Intersection of multiple union types does not narrow discriminated field[/-][+]Intersection of multiple union types does not narrow properties[/+]jack-williams commentedon 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
andtypeBar
. As a consequence, fieldz
is not considered excess infoo & bar
because it appears in at least on part of the union.typescript-bot commentedon Mar 10, 2019
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.