microsoft / microsoft/TypeScript

Types with optional props get implicit index signatures that cannot be made explicit

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

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

In Discussion Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

🔎 Search Terms

implicit index signatures, explicit index signatures, optional partial undefined,

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

Playground link

💻 Code
// @strict: true
// @exactOptionalPropertyTypes: false
// @noUncheckedIndexedAccess: false

type ObjImplicit = {
    a?: string | undefined,
    b: string
};
type Idx = { [k: string]: string }
declare const o: ObjImplicit;
const i: Idx = o; // allowed because A has implicit index signature

type ObjExplicit = {
    a?: string | undefined, // error! not allowed
    //~ <-- Property 'a' of type 'string | undefined' is not assignable to 'string' index type 'string'.
    b: string,
    [k: string]: string
}
🙁 Actual behavior

{ a?: string | undefined } can implicitly have the index signature {[k: string]: string} but cannot explicitly have such an index signature

🙂 Expected behavior

I think I expected the implicit index signature to fail?

Additional information about the issue

Without --exactOptionalPropertyTypes, {a?: string} and {a?: string | undefined} are the same type. I figured that would mean both implicitly and explicitly adding index signatures would either require or not require undefined.

We also have the situation where Partial<{[k: string]: string}> becomes {[k: string]: string | undefined} instead of {[k: string]: string}, which makes me lean toward "require undefined", for consistency. Currently the discrepancy leads to questions like this one.

Anyway, I expect this to not be a bug, but intended behavior that flew under my radar and which I cannot see explicitly documented anywhere. Mostly I just want to understand this behavior so I can explain it when people ask questions like the one linked above.

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

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

はじめの一歩

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

調査の方向性

リンク先の TypeScript Playground から始め、表示されているコンパイラーオプションでの暗黙的および明示的なインデックスシグネチャの例を比較してください。意図されたルールを特定するために、関連する型チェックの動作を追跡し、そのルールと exactOptionalPropertyTypes および Partial との関係を文書化してください。相違点が明確に説明されていれば完了です。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
ドキュメント
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
35/100

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

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