Skip to content

Intersection of multiple union types does not narrow properties #30255

Closed
@Haubach

Description

@Haubach

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

Activity

changed the title [-]Intersection of multiple union types does not narrow discriminated field[/-] [+]Intersection of multiple union types does not narrow properties[/+] on Mar 7, 2019
jack-williams

jack-williams commented on Mar 7, 2019

@jack-williams
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.

typescript-bot

typescript-bot commented on Mar 10, 2019

@typescript-bot
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Haubach@jack-williams@RyanCavanaugh@typescript-bot

        Issue actions

          Intersection of multiple union types does not narrow properties · Issue #30255 · microsoft/TypeScript