Skip to content

Tagged type narrowing doesn't work when one of the tags is a union type #43026

Closed
@andriy-kudrya

Description

@andriy-kudrya

Bug Report

Tagged type narrowing doesn't work when one of the tags is a union type. Sorry, not sure how to be more descriptive

🔎 Search Terms

union narrowing

🕗 Version & Regression Information

3.9.7, 4.2.2, Nightly

  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about common bugs that aren't bugs
  • I was unable to test this on prior versions because _______

⏯ Playground Link

Playground link with relevant code

💻 Code

interface A {
    type: 'a'
}

interface BC {
    type: 'b' | 'c'
}

declare const value: A | BC

let a: A

if (value.type === 'a')
    a = value

if (value.type !== 'b' && value.type !== 'c')
    // type narrowing doesn't work here, value has type "A | BC" instead of "A"
    a = value

declare function isA(x: 'a' | 'b' | 'c'): x is 'a'

if (isA(value.type))
    // type narrowing doesn't work with type guard either
    a = value

🙁 Actual behavior

Type of a is not narrowed to A

🙂 Expected behavior

Type of a is narrowed to A

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