microsoft / microsoft/TypeScript
Allow creating contantly typed Map which has immutable defined set of keys
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Review the issue's proposed as const behavior for Map, including readonly operations and key-specific get results. Define the type-system semantics and identify the relevant compiler entry points and tests before determining whether the requested behavior can be implemented consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100