microsoft / microsoft/TypeScript
enum to string (and number) literals keeping the opacity
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Search Terms
enum typing, enum valueOf, enum string literal
Suggestion
The values of the strings enum are by design opaque it might however be useful to have a way to type them out of the enum.
This is what we have now:
const enum anEnum {
a = 'A',
b = 1,
}
type keysName = keyof typeof anEnum // "a" | "b"
type enumValues = typeof anEnum[keysName]; // anEnum.a | anEnum.b
type enumValueOf = ReturnType<enumValues['valueOf']>; // string | number
const aString: string = anEnum.a.valueOf(); // okay
const aNumber: number = anEnum.b.valueOf(); // okay
const notString: number = anEnum.a.valueOf(); // Type 'string' is not assignable to type 'number'.
const notNumber: string = anEnum.b.valueOf(); // Type 'number' is not assignable to type 'string'.
My feature request is to type the valueOf() using string literals and number "literals"
enum anEnum {
a = 'A',
b = 1,
}
type keysName = keyof typeof anEnum // "a" | "b"
type enumValues = typeof anEnum[keysName]; // anEnum.a | anEnum.b
type enumValueOf = ReturnType<enumValues['valueOf']>; // 'A' | 1
const aString: 'A' = anEnum.a.valueOf(); // okay
const aNumber: 0 = anEnum.b.valueOf(); // okay
const notString: number = anEnum.a.valueOf(); // Type 'A' is not assignable to type 'number'.
const notNumber: string = anEnum.b.valueOf(); // Type 0 is not assignable to type 'string'.
Use Cases
- this is stricter and therefore never bad
- this would allow having the values of an enum which would be useful to compare enums at compilation time (without messing with the opacity of the enum itself.)
Examples
const enum anEnum {
a = 'A',
b = 1,
}
const enum anotherEnum {
a = 'A',
b = 1,
}
// This is not allowed despite both enums are the same
const value: anotherEnum = anEnum.a;
// This would be allowed and safe (even with const enum)
type anotherEnumValues = ReturnType<typeof anotherEnum[keyof typeof anotherEnum]['valueOf']>;
const anOtherEnumValues: anotherEnumValues = anEnum.a;
Note: What I do not know is how this custom type could use a generic for the enum, as <T extends enum> is invalid. An enum is a bit weird in TS as it both represents a type and an object
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
Non sono stati identificati file del repository, test o punti di ingresso dell'implementazione. Inizia esaminando il typing degli enum e gli esempi di valueOf nell'issue, quindi traccia come i tipi dei membri degli enum vengono rappresentati e testati nel compilatore TypeScript. Il lavoro è completo quando i risultati di valueOf() degli enum stringa e numerici conservano i loro tipi letterali senza modificare l'output a runtime né l'opacità degli enum.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- 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