microsoft / microsoft/TypeScript
Confusing error message (2322), should use (2741) and (2322) error message when this["XXX"] = {...} has a missing (or mispelled) key or an incompatible value.
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
### 🔍 Search Terms
Relevant issues:
- https://github.com/microsoft/TypeScript/issues/29049
### ✅ Viability Checklist
- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
### ⭐ Suggestion
When we have a Record `this["faa"]` to which we affect an object with an incompatible type, e.g.:
- a key is missing (or misspelled).
- one value has the wrong type.
The following error is shown:
> Type '{}' is not assignable to type 'this["faa"]'.
> 'this["faa"]' could be instantiated with an arbitrary type which could be unrelated to '{}'.(2322)
> input.tsx(3, 19): 'key' is declared here.
In this context, I'd like the error message to be modified in order to be less confusing, i.e. :
- if a key is missing: _"Property 'key' is missing in type '{}' but required in type '{ key: number | null; }'.(2741)"_
- if one value has a wrong type: _"Type 'string' is not assignable to type 'number'.(2322)"._
### 📃 Motivating Example
[Playground Link](https://www.typescriptlang.org/play/?ts=6.0.0-dev.20260224#code/MYGwhgzhAECC0G8BQ1XQCYFNRgE6egDMB7YgLmgBcALASwgG0AiQsMJgXQG4U0sd8RNhQQBrTAE8KAOwCuAWwBGmXAB85IEAF8kvVIVmyAFAEpEetKhr0AdCWLQAvIh2XL1iHdJPE4qdCYISlwmVzcrOk97HzFJCgAWACYw1B0dJBBMSiJSET8ZBWU1DW0eaOcEHXLfOICgkKrvCvzoJJ0gA)
```ts
class A {
declare foo: this["faa"];
declare faa: {key: number|null}
fuu() {
// Type '{}' is not assignable to type 'this["faa"]'.
// 'this["faa"]' could be instantiated with an arbitrary type which could be unrelated to '{}'.(2322)
// input.tsx(3, 19): 'key' is declared here.
this.foo = {}
// Type '{ key: string; }' is not assignable to type 'this["faa"]'.
// 'this["faa"]' could be instantiated with an arbitrary type which could be unrelated to '{ key: string; }'.(2322)
this.foo = {key: "str"}
this.foo = {key: 42} // ok
}
}
let foo: {key: number|null};
// Property 'key' is missing in type '{}' but required in type '{ key: number | null; }'.(2741)
// input.tsx(12, 11): 'key' is declared here.
foo = {}
// Type 'string' is not assignable to type 'number'.(2322)
// input.tsx(12, 11): The expected type comes from property 'key' which is declared here on type '{ key: number | null; }'
foo = {key: "str"}
foo = {key: 42} // ok
```
It is easy to forget a key or to misspell it. Unfortunately, the error message is currently confusing.
When playing with `this["faa"]` we often encounter this error, and we might look at it too quickly, missing (or misunderstanding) the last line, thinking this is some kind of TS limitation.
### 💻 Use Cases
1. What do you want to use this for?
Having more explicit error messages.
2. What shortcomings exist with current approaches?
Confusing error message.
3. What workarounds are you using in the meantime?
N/A
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた Playground と issue の動機となった代入から始め、次にそれらの診断をスタンドアロンの `foo` の例と比較し、関連する issue #29049 を確認します。TypeScript がプロパティの欠落またはプロパティ型の診断ではなく、汎用的な `this["faa"]` の診断を選択する箇所を追跡します。2 つの互換性のない代入が、正当なコードや実行時の出力を変更せずに、より具体的な期待されるメッセージを報告すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 活発
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100