Potentially-`undefined` value not flagged when destructuring union of empty tuple and array
#61 424 ouverte le 14 mars 2025
Métriques du dépôt
- Stars
- (48 455 stars)
- Métriques de merge PR
- (Merge moyen 6j 17h) (9 PRs mergées en 30 j)
Description
🔎 Search Terms
"tuple destructuring", "tuple narrowing"
🕗 Version & Regression Information
This bug appears to occur as far back as TS 3.3 up to the current nightly build, as tested in the Playground.
⏯ Playground Link
💻 Code
type Arr = Array<number> | [] | [string];
declare const arr: Arr;
const [a] = arr;
a.toString(); // Expected: error. Actual: no error. ❌
const b = arr[0];
b.toString(); // Expected: error. Actual: error. ✅
.d.ts from Playground:
type Arr = Array<number> | [] | [string];
declare const arr: Arr;
declare const a: string | number | undefined;
declare const b: string | number | undefined;
🙁 Actual behavior
When destructuring a value whose type is an array, an empty tuple, and a non-empty tuple, the destructured value isn't flagged as possibly undefined which caused a runtime error. The value is correctly flagged as possibly undefined when accessing it by index.
🙂 Expected behavior
When destructuring a value whose type is an array, an empty tuple, and a non-empty tuple, the destructured value should be flagged as potentially being undefined.
Additional information about the issue
This seems related to #55661 except, in this case, the union contains a mix of tuples and an array.