Skip to content

Selecting a case from a discriminated union using intersection does not work #22154

Closed
@rklaehn

Description

@rklaehn

TypeScript Version: 2.7.0-dev.201xxxxx

Search Terms:
discriminated union, narrowing, intersection

Code

type DU = { t: '1', a: 2 } | { t: '2', c: 3 }
type C1 = DU & { t: '1' }
declare const foo: C1
foo.a // <= does not compile

Expected behavior:
C1 should be inferred to { t: '1', a: 2 }, so it should be clear that a can be accessed

Actual behavior:
C1 is inferred to ({ t: "1"; a: 2; } & { t: "1"; }) | ({ t: "2"; c: 3; } & { t: "1"; })

Note the impossible type ({ t: "2"; c: 3; } & { t: "1"; }) is not removed!

Playground Link:
link

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions