Skip to content

Union type not correctly narrowing down #44479

Closed
@danielbartsch

Description

@danielbartsch

Bug Report

🔎 Search Terms

union narrowing wrong

🕗 Version & Regression Information

in 3.7.5. still there in 4.3.2

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "bugs that aren't bugs"
  • I was unable to test this on prior versions because prior versions don't support ??

⏯ Playground Link

Playground link with relevant code

💻 Code

type A = { a: number, b: string }[]
type B = { a: number }[]

declare var c: A | B | undefined;

const d = c ?? []

// d is now just B, but should be A | B

🙁 Actual behavior

Type inference excludes a part in the intersection type.

🙂 Expected behavior

Type inference should just exclude undefined, because the ?? operator was used on the variable.

Activity

fatcerberus

fatcerberus commented on Jun 7, 2021

@fatcerberus

That's a union type, not an intersection.

danielbartsch

danielbartsch commented on Jun 7, 2021

@danielbartsch
Author

whoops. You're right. With that correction, I was able to find related issues:
#44401
#43825
#43667

which are almost the same, but not quite.

changed the title [-]Intersection type not correctly narrowing down[/-] [+]Union type not correctly narrowing down[/+] on Jun 7, 2021
MartinJohns

MartinJohns commented on Jun 7, 2021

@MartinJohns
Contributor
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

        @fatcerberus@MartinJohns@RyanCavanaugh@danielbartsch

        Issue actions

          Union type not correctly narrowing down · Issue #44479 · microsoft/TypeScript