microsoft / microsoft/TypeScript
Allow creating contantly typed Map which has immutable defined set of keys
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Esamina il comportamento as const proposto nell’issue per Map, incluse le operazioni readonly e i risultati di get specifici per chiave. Definisci la semantica del sistema dei tipi e individua i punti di ingresso rilevanti del compilatore e i test prima di determinare se il comportamento richiesto può essere implementato in modo coerente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100