Bug Report
When narrowing the type of a dependent parameter of a tuple union by checking the discriminator, the type is no longer narrowed inside of, and after, a loop. This works correctly when destructuring an object, but not a tuple.
🔎 Search Terms
destructured union loop
destructured tuple union
array union type loop
destructured union dependent loop
dependent type loop
🕗 Version & Regression Information
- This is the behavior in every version I tried (this feature was introduced in 4.6)
⏯ Playground Link
Playground link with relevant code
💻 Code
declare function fn(): [true, number] | [false, string];
const [guard, value] = fn();
value // type: string | number
if (guard) {
value // type: number
if (true) {
value // type: number
}
for (;;) {
value // type: string | number
}
} else {
value // type: string
if (true) {
value // type: string
}
while (0) {
value // type: string | number
}
}
🙁 Actual behavior
value is the correct type in the if statement, but is incorrect inside the loop and after it (no longer narrowed). It's assigned using const, so we know that this is wrong since the value can't change.
🙂 Expected behavior
value should be the same type inside the loop as it is outside the loop.
Bug Report
When narrowing the type of a dependent parameter of a tuple union by checking the discriminator, the type is no longer narrowed inside of, and after, a loop. This works correctly when destructuring an object, but not a tuple.
🔎 Search Terms
destructured union loop
destructured tuple union
array union type loop
destructured union dependent loop
dependent type loop
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
valueis the correct type in theifstatement, but is incorrect inside the loop and after it (no longer narrowed). It's assigned usingconst, so we know that this is wrong since the value can't change.🙂 Expected behavior
valueshould be the same type inside the loop as it is outside the loop.