microsoft / microsoft/TypeScript

Unions seem to sidestep `--noUncheckedIndexedAccess` and lose the possibility of undefined

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

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

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

説明

### 🔎 Search Terms

noUncheckedIndexedAccess, union, undefined

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about `--noUncheckedIndexedAccess`

### ⏯ Playground Link

[Playground link](https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true#code/MYewdgzgLgBGCuBbCAuGBvGBtA1m6ATgJZgDmAumgogEYCmBMAvjALwwCyAhlABYB0BLmAAmIRAAoAlDAA8MAAz8ArDAD8GGFzQBGZjDSYaaAEzMA3AChQkWITabtMAES86AG3chnzS5YD0-jAA7kR8MAC0EWAgAKpgwG7AOHQiAJKidAAeqQCCwMB0EBAw4Nbg0DC8iIgOEtx8gsJikjLySqoa1BD8XAYwhL0ygQNQxGQwAD5wSPSM0-CZAGYkqeW2Ibx97PU8AkKi4tJyiirqM8j9hFK9MCOEJKRTF3N+QA)

### 💻 Code

```ts
const nums: { [k: string]: number } = Math.random() < 0.5 ? { a: 1 } : { b: 2 };
const str = { a: "hello" }

// with --noUncheckedIndexedAccess on
const hmm = (Math.random() < 0.5 ? nums.a : str.a) // string | number | undefined
const wha = (Math.random() < 0.5 ? nums : str).a // string | number <-- 😕
```

### 🙁 Actual behavior

The type of `wha` is `string | number`, completely ignoring the possibility that it might be `undefined`, even though `--noUncheckedIndexedAccess` is enabled. Looks like unions of types with index signatures and known keys lose the `--noUncheckedIndexedAccess` behavior.

### 🙂 Expected behavior

`wha` should be of type `string | number | undefined`, just like `hmm`, since indexed access into a union should look like a union of indexed accesses.

### Additional information about the issue

This is related to #50474. It’s also related to #47531, which had specifically to do with `never[]` and so the focus was on avoiding `never[]` as opposed to investigating what happened to the indexed access. ( https://github.com/microsoft/TypeScript/issues/47531#issuecomment-1229522810 )

Ran into this when looking at [a Stack Overflow question](https://stackoverflow.com/q/79451704/2887218)

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

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

はじめの一歩

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

調査の方向性

--noUncheckedIndexedAccess を使用した、リンク先の TypeScript Playground の再現コードから始め、issue にある 2 つのインデックスアクセス式を比較してください。issue ではリポジトリのファイルもテストも指定されていません。ユニオンに対するインデックスアクセスがどのように解決されるかを追跡し、その後、2 つ目の式に undefined が含まれることを示すカバレッジを追加して、関連するコンパイラテストを実行してください。

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

評価

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

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

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