microsoft / microsoft/TypeScript

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

オープン
#61,424 コメント 4 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Domain: check: Control Flow Help Wanted
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンクされた TypeScript Playground の再現例から始め、タプルの分割代入で推論される型と、インデックスアクセスの結果を比較してください。背景については、関連する issue #55661 を確認してください。この配列とタプルのユニオンの場合に、分割代入された値が undefined になる可能性があると報告され、正しいインデックスアクセスの動作が変更されていなければ完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。