Skip to content

Intersection of discriminated union (of records) and a record is incorrect #47612

Closed
@RebeccaStevens

Description

@RebeccaStevens

Bug Report

🔎 Search Terms

intersection with discriminated union

This might be a duplicate of #33654 but I'm not completely sure. This issue has a simpler example anyway.
It also seems to be a duplicate of #9919 which was reportedly fixed. This is either regression or a missed case.

🕗 Version & Regression Information

  • This is the behavior in every version I tried (v3.3 and v4.5), and I reviewed the FAQ for entries about intersection with discriminated union.

⏯ Playground Link

Playground link with relevant code

💻 Code

type X = ({ a: string } | { b: number }) & { a: unknown };
type Y = X["a"]; // actual: unknown, expected: string

🙁 Actual behavior

type Y is unknown.

🙂 Expected behavior

type Y should be string.

Why:

  • When discriminating { a: string } | { b: number } with "a must be defined as a field", we get { a: string }.
  • { a: string } & { a: unknown } can be simplified to { a: string }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions