question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Narrow subtype-reduction-prone unions to their narrowest constituent

See original GitHub issue

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? https://github.com/microsoft/TypeScript/issues/43584#issuecomment-816932485

🔎 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[]

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
RyanCavanaughcommented, Feb 4, 2022

You could write

declare let a: { push?: never } | 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()
}

Subtype-reducing unions aren’t great but it seems like we could fix the narrowing-on-assignment logic to do a subtype reduction on any resulting union, since that’s assured to be sound.

3reactions
fatcerberuscommented, Feb 4, 2022

This is expected; arrays are assignable to object.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unions 101 | U.S. Department of Labor
A labor union is a group of two or more employees who join together to advance common interests such as wages, benefits, schedules...
Read more >
Unions help narrow the gender wage gap
Unions provide a boost to women regardless of their race or ethnicity. The gender wage gap is significantly smaller among both white and...
Read more >
Turning the Tables: Participation and Power in Negotiations
We sought unions that hold elections for their negotiations' teams. We looked for unions that have big bargaining teams versus small ones.
Read more >
The Closing Gender, Education, and Ideological Divides ...
Unions have narrowed class and ideological divides to make Generation Z America's most pro-union generation.
Read more >
Union Members - 2021 - Bureau of Labor Statistics
The gap between union membership rates for men and women has narrowed considerably since 1983. (the earliest year for which comparable data ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found