microsoft / microsoft/TypeScript
Writes to indexed access of an index signature are not sufficiently constrained
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
"return type" "generic" "type parameter" "indexed access" "index signature" "assignable" "constraint"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about index signatures
⏯ Playground Link
💻 Code
type Example = {
a: 'a'
[key: string]: string
}
declare const example: Example
const key1: string = 'a'
example[key1] = 'not a' // no error
declare const key2: 'a' | 'b'
example[key2] = 'not a' // error, as expected
//^^^^^^^^^^^
// Type '"not a"' is not assignable to type '"a"'.
🙁 Actual behavior
The index signature allows any string value to be assigned, ignoring the narrower type of a.
🙂 Expected behavior
Both property assignments in the above code should raise errors, for the same reason.
Additional information about the issue
This is a more generalized version of #60700. @MartinJohns helped me realize that type parameters need not be involved.
Here's another pair of examples (1, 2) which I expected to be reasoned through similarly:
type WiderType = { a?: string, b?: string }
type Example = { a: 'a' } & WiderType
declare const example: Example
declare const key: keyof Example
example[key] = 'not a'
//^^^^^^^^^^
// Type '"not a"' is not assignable to type '"a"'.
type WiderType = { [key: string]: string }
type Example = { a: 'a' } & WiderType
declare const example: Example
declare const key: keyof Example
example[key] = 'not a' // no error
In both cases Example carries with it the information that the property a must have a value assignable to 'a' (as can be seen with example.a = 'not a'). But this is not enforced when assigning to dynamic property using a key whose type matches the index signature.
Here's another example which does produce the error I expect:
type WiderType = { [key: string | symbol]: string }
type Example = { [key: string]: 'a' } & WiderType
declare const example: Example
declare const key: string | symbol
example[key] = 'not a' // error, as expected
//^^^^^^^^^^
// Type '"not a"' is not assignable to type '"a"'.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされている TypeScript Playground の例から始め、string キーを介したインデックス付き代入と、プロパティへの直接代入を比較します。Type Checker がインデックスアクセスに対する書き込み可能な型をどのように決定するかを追跡し、より狭く宣言されたプロパティが強制されることを示すカバレッジを追加します。完了の条件は、報告された両方の例が "not a" を拒否し、既存の有効なケースは引き続き受け入れられることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 35/100