TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Potentially-`undefined` value not flagged when destructuring union of empty tuple and array

Open bthall16 opened this issue 8 months ago • 4 comments

🔎 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

https://www.typescriptlang.org/play/?ts=5.8.2#code/C4TwDgpgBAggTnKBeWCCGIA8A7ArgWwCMI4A+KAHygG0BdSmgZ2DgEtsBzWgbgCheAJhADGAGzRxowgPbZmUCXABcqOH14y5wGmnopFfNADpg0gMot2HABQBKblAD0jqAFEAHpGHAIAlSThpOCNYb1w0URVsaSgAoJDAGXJ+TXlCZAUEagAGHl5CE3NLTjsHZzdPER8-WIR40OBwyJrA4KhAUHJ+XigoIA

💻 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.

bthall16 avatar Mar 14 '25 19:03 bthall16

You need to enable noUncheckedIndexedAccess. That's what the flag is there for.

MartinJohns avatar Mar 14 '25 19:03 MartinJohns

Still weird that the destructuring behavior is different though.

RyanCavanaugh avatar Mar 14 '25 19:03 RyanCavanaugh

Even with noUncheckedIndexAccess off, the index access in the snippet works correctly: b could be undefined and TS correctly raises an error when I try to call toString. It's the destructuring behavior that seems incorrect since there's no error trying to call toString when there should be.

EDIT: I suppose it's not accurate for me to say the index access works "correctly" when noUncheckedIndexAccess is off (as in the snippet). Rather, it works "intuitively" but I'm not sure what "correct" behavior would look like here since arrays and tuples would individually behave differently.

bthall16 avatar Mar 14 '25 20:03 bthall16

An extra bug related to this:

type Arr = Array<number> | [] | [string];

declare const arr: Arr;

export const [a] = arr;

type Mix = Iterable<number> | [] | [string];

declare const mix: Mix;

export const [b] = mix;

Actual .d.ts emit:

export declare const a: string | number | undefined;
export declare const b: string | number;

Expected .d.ts emit:

export declare const a: string | number | undefined;
export declare const b: string | number | undefined;

Andarist avatar Mar 14 '25 21:03 Andarist