microsoft / microsoft/TypeScript

Destructuring assignment with computed property name does not help narrow types as intended.

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

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

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

説明

### 🔎 Search Terms

"destructuring assignment", "computed property name", "type narrowing"

### 🕗 Version & Regression Information

Happen to all existing version in typescript playground.

### ⏯ Playground Link

https://tsplay.dev/w1eElw

### 💻 Code

```ts
declare let obj: Record

declare let cond: boolean;
declare let key: string;
let result: string | null = null;

if (cond) {
({ [key]: result } = obj);
result;
// ^?
}

if (cond) {
({ 'key': result } = obj);
result;
// ^?
}
```

### 🙁 Actual behavior

Type narrowing did not happen to the assignee variable `result` after the assignment `({ [key]: result } = obj);`.

### 🙂 Expected behavior

Expecting the assignment `({ [key]: result } = obj);` will narrow the assignee variable `result` to non-null.

### Additional information about the issue

_No response_

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground の再現コードから始め、computed-property の分割代入と literal-property の代入の後における result の推論された型を比較します。関係する型の絞り込みの挙動を追跡し、完了した変更によって両方の例で期待される non-null の絞り込みが維持されることを確認します。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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