microsoft / microsoft/TypeScript
Nominal/branded key for `Record` disables checking/inferring of value type
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Bug Report
🔎 Search Terms
nominal, Record, branded
🕗 Version & Regression Information
3.3.3 through 4.3.2
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about nominal types and record types
⏯ Playground Link
Playground link with relevant code
💻 Code
type Id = string & { __id: 'id' }
type User = {
id: Id;
name: string;
}
type Users = Record<Id, User>;
type Users2 = Map<Id, User>;
type Users3 = Record<string, User>;
// This will not produce an error
// @ts-expect-error No name should be an error!
const users: Users = { id0: { id: 'id', } }
// type: [string, unknown][]
const entries = Object.entries(users);
const users2: Users2 = new Map();
// @ts-expect-error No name produces an error
users2.set('id' as Id, { id: 'id' as Id, })
// @ts-expect-error No name produces an error
const users3: Users3 = { id0: { id: 'id' as Id, } }
// type: [string, User][]
const entries3 = Object.entries(users3);
🙁 Actual behavior
Using a nominal/branded type as the key to a record type causes type checking of the value to be skipped. Additionally, inference will also fail and fallback to unknown, such as using Object.entries. Using a Map works as expected, but not a Record. I realize that the runtime behavior between a Map (real) and Record (type) is completely different, I'm just providing it as an example.
🙂 Expected behavior
I'd expect types to be preserved and checked, even when using a nominal/branded type as a key to a record.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、リンクされた TypeScript Playground の再現コードを Record<Id, User> と Object.entries の例で実行し、動作する Map と文字列キーの Record のケースと比較します。コンパイラが branded key と record value をどのように処理するかを追跡します。値のチェックで不足している name が報告され、Object.entries(users) が unknown を推論するのではなく User を保持すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 38/100