-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Open
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysisThe issue relates to control flow analysisHelp WantedYou can do thisYou can do this
Milestone
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.
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysisThe issue relates to control flow analysisHelp WantedYou can do thisYou can do this