Skip to content

Discriminated union intersected with discriminant does not join to the original type when discriminated #36333

@jack-williams

Description

@jack-williams

TypeScript Version: master

Search Terms:

Discriminated union intersection narrowing

Code

type A = { key: "one", value: number } | { key: "two", value: string };

type A_one = A & { key: "one" };

function foo(a1: A_one): number {
    if (a1.key) {

    }
    return a1.value;  // no error
}

function bar(a1: A_one): number {
    return a1.value;  // error
}

Expected behavior:
Consistent behavior between foo and bar, though I'm not sure how easy that is.

  • Making them both error feels like a regression.
  • Making them both ok would need to introduce some new logic for arbitrary property access.

Actual behavior:
The control flow through the joint point is doing some type reduction not present in the plain version.

Playground Link:

link

Related Issues:

Found when looking at #36323.

This was (probably) introduced by the fix at #35513.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions