Narrow subtype-reduction-prone unions to their narrowest constituent
See original GitHub issueBug 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
💻 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:
- Created 2 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You could write
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.
This is expected; arrays are assignable to
object
.