Skip to content

Narrow subtype-reduction-prone unions to their narrowest constituent #47731

Open
@paul-marechal

Description

@paul-marechal

Bug Report

Type narrowing doesn't seem to take into account an assignment I made to ensure the variable has the right type.

Maybe related? #43584 (comment)

🔎 Search Terms

type narrowing assignment

🕗 Version & Regression Information

4.5.4

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "type narrowing"

⏯ Playground Link

Playground Link

💻 Code

declare let a: object | any[] | undefined

if (a === undefined) {
    a = [] // I expect this line to have TS understand that a is any[] from now on
} else if (!Array.isArray(a)) {
    throw new Error()
}
[...a] // complains here because a is object | any[]

🙁 Actual behavior

a has type object | any[]

🙂 Expected behavior

a to have type any[]

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions