microsoft / microsoft/TypeScript
Optional chaining should always include `undefined` in its resolved type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
TypeScript Version: 3.7.5
Search Terms: optional chaining return type
Code
I have a formatting function, where the input should always be a string, but in certain cases, from the backend, it's undefined for some reason. So to make sure the front-end at least doesn't crash when that happens, I added optional chaining, which fixed the issue.
But the types are kind of off now, and I didn't realize until now because Typescript didn't complain about anything.
export const formatAccountNumber = (account: string): string =>
account?.replace(/^(\d{4})(\d{2})(\d{5})/, '$1 $2 $3');
Expected behavior: Should get an error/warning about something here. Preferably, I guess optional chaining should always expand the type to include undefined to whatever it is added to? Then, in this example, Typescript could say that the return type is string, but I'm trying to return string | undefined? Either that, or maybe it shouldn't be possible to use optional chaining on a value that does not include either the type undefined and/or null?
Actual behavior: No warnings or errors.
Related Issues: no
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 riproducendo l'esempio TypeScript 3.7.5 con optional chaining e il suo tipo di ritorno string esplicito. Determina se la correzione prevista consiste nell'includere undefined nel tipo risolto o nel rifiutare optional chaining su valori non nullable; il lavoro è completato quando il comportamento scelto viene applicato in modo coerente ed è coperto dai test del compilatore.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100