microsoft / microsoft/TypeScript
Destructuring object with generic key should reject undefined
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
destructure, generic key, undefined
🕗 Version & Regression Information
This is the behavior in every version I tried (back to at least 5.0), and I reviewed the FAQ for entries about destructuring
⏯ Playground Link
💻 Code
interface T { a?: unknown; b?: unknown; c?: unknown }
function f<L extends keyof T>(k: L, t: T | undefined): unknown {
const { [k]: v } = t // should error (crashes if t undefined)
const _v2 = t[k] // equivalent code, does error
return v
}
🙁 Actual behavior
TS does not error on the destructuring, even though it will crash at runtime if t is undefined, and it errors on equivalent code using object-access syntax.
🙂 Expected behavior
TS should error on the indicated line.
Additional information about the issue
This is simplified from an actual example in our codebase. Obviously in this case it's silly to destructure but this was part of a larger block of destructuring so the code would at a glance seem reasonable.
Note that the destructured value does end up having a funky type (T | undefined)[L], which presumably it's hard to do anything with. But of course it's still assignable to unknown, and thus usable in various ways. (In the actual code in question, the authors were doing other complicated things, running an issue similar to #40312, and ended up casting through unknown to the type they wanted.)
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた Playground の例から始め、checker が computed-property の分割代入を、対応するインデックスアクセス式とどのように扱うかを比較します。型チェックで generic key と possibly undefined なオブジェクトを追跡します。分割代入の行に適切なエラーが付与され、有効な分割代入にリグレッションが発生しなければ完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 38/100