microsoft / microsoft/TypeScript
Allow creating contantly typed Map which has immutable defined set of keys
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
Search Terms
map, const, es6
Suggestion
Currently you can create objects and arrays with the as const assertion, which tells the compiler to, for example, type ["apple", "banana", "pear"] as ["apple", "banana", "pear"] rather than as string[]. This is useful for instances where the array is constant and you know you can rely on that.
Typing objects as const is useful for constant maps/dictionaries, however the better type to use in TypeScript for maps is, of course, Map. However, you can't type Map as const, which means you can never have a constant Map that disallows set and delete and also knows whether the result of get is undefined or not, rather than always making it return the type x | undefined.
Note: I know I can just use an object for this instead of a Map. But Map offers many benefits, such as speed and iterability, that objects don't. Also, Map is more explicit about what it is doing.
Examples
Without this feature, I have to do one of the following:
const optionA: Map<string, string> = new Map([
["date", "YYYY-MM-DD"],
["datetime", "YYYY-MM-DD[T]HH:mm"],
["datetime-local", "YYYY-MM-DD[T]HH:mm"],
["month", "YYYY-MM"],
["time", "HH:mm"],
["week", "YYYY-[W]WW"]
]);
const weekFormat = optionA.get("week") as string; // Forced to use assertion and we still lose data
or
const optionB = {
date: "YYYY-MM-DD",
datetime: "YYYY-MM-DD[T]HH:mm",
"datetime-local": "YYYY-MM-DD[T]HH:mm",
month: "YYYY-MM",
time: "HH:mm",
week: "YYYY-[W]WW]
} as const; // Have to use object
const weekFormat = optionA.week;
With this feature, I could do:
const optionA = new Map([
["date", "YYYY-MM-DD"],
["datetime", "YYYY-MM-DD[T]HH:mm"],
["datetime-local", "YYYY-MM-DD[T]HH:mm"],
["month", "YYYY-MM"],
["time", "HH:mm"],
["week", "YYYY-[W]WW"]
]) as const;
const weekFormat = optionA.get("week"); // type: "YYYY-[W]WW"
optionA.delete("time"); // error
optionA.set("time", "hh:mm"); // error
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Issue で提案されている Map の as const の挙動を、readonly 操作とキー固有の get 結果を含めて確認します。要求された挙動を一貫して実装できるかどうかを判断する前に、型システムのセマンティクスを定義し、関連するコンパイラーのエントリーポイントとテストを特定します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100