Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intersection of multiple union types does not narrow properties #30255

Closed
Haubach opened this issue Mar 7, 2019 · 2 comments
Closed

Intersection of multiple union types does not narrow properties #30255

Haubach opened this issue Mar 7, 2019 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Haubach
Copy link

Haubach commented Mar 7, 2019

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

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

@Haubach 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
@jack-williams
Copy link
Collaborator

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.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 7, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants