microsoft / microsoft/TypeScript
Allow creating contantly typed Map which has immutable defined set of keys
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Examinez le comportement as const proposé dans l’issue pour Map, y compris les opérations readonly et les résultats de get spécifiques à la clé. Définissez la sémantique du système de types et identifiez les points d’entrée pertinents du compilateur et les tests avant de déterminer si le comportement demandé peut être implémenté de manière cohérente.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript, typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100