microsoft / microsoft/TypeScript

`noUncheckedIndexedAccess` should forbid unsound `Record<string, string>` → `Record<"k", string>` coercion

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

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

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

🔎 Search Terms

coercion, noUncheckedIndexedAccess, Record, unsound

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about noUncheckedIndexedAccess
⏯ Playground Link

https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&ts=5.9.3#code/DYUwLgBAhgXBBKIDGB7ATgEwDwGcxoEsA7AcwBoI9DSA+CAXggG8BfAbgChRIAjORVJiwAiANbCKVYiTqMobCAHpFEAK5EcKdRi7gIPUXCmkG+gHSjOBs2BQAZFAHcQaAMJQcIABQBKBcogAMygCYBxoSDR1MAIAWxAOIA

💻 Code
let a: Record<string, string> = {};
let b: Record<"k", string> = a; // unsound
let bk: string = b.k;
bk.toLowerCase(); // fails at runtime
🙁 Actual behavior

No TypeScript errors.

🙂 Expected behavior

By enabling noUncheckedIndexedAccess, I’ve opted in to stricter errors that prevent mistakes where a potentially undefined record element is assumed to be defined. The coercion from a: Record<string, string> to Record<"k", string> is such a mistake, since it assumes that a.k is defined. So TypeScript should forbid this coercion when noUncheckedIndexedAccess is enabled.

Additional information about the issue

noUncheckedIndexedAccess already causes TypeScript to correctly reject the equivalent unsound coercion Record<string, string>{k: string}.

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

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

はじめの一歩

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

調査の方向性

リンク先の TypeScript Playground の例を noUncheckedIndexedAccess を有効にして再現し、次に Record<string, string> から Record<"k", string> へのケースを、すでに拒否されている { k: string } への同等の coercion と比較します。noUncheckedIndexedAccess の下で安全でない Record の coercion が拒否され、issue に記載された期待される動作が変更されなければ完了です。

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

評価

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

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

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