microsoft / microsoft/TypeScript
Type of reverse mappings of numeric enums is a `string` instead of `keyof typeof Enum`
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
TypeScript Version: 4.0.0-dev.20200526
Search Terms:
enum
numeric enum
reverse enum
Code
enum Enum {
one = 1,
two = 2,
}
const a: keyof typeof Enum = Enum[Enum.one]; // (1)
const b = Enum[0]; // (2)
console.log(a, b);
Expected behavior:
When a numeric enum is indexed by itself the value should be of type keyof typeof Enum:
No error on line (1).
Error Property '0' does not exist on type 'typeof Enum' on line (2).
Actual behavior:
When a numeric enum is indexed by a number (including itself) the value is of type string:
Error Type 'string' is not assignable to type '"one" | "two"' on line (1).
No error on line (2).
Playground Link: Provided
Related Issues:
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
Inizia con il link Playground fornito e la riproduzione minima di un enum numerico nell’issue. Verifica i tipi di accesso indicizzato rispetto al comportamento previsto: Enum[Enum.one] dovrebbe essere keyof typeof Enum, mentre Enum[0] dovrebbe segnalare che la proprietà 0 non esiste su typeof Enum.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 35/100