microsoft / microsoft/TypeScript
Optional chaining should always include `undefined` in its resolved type
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
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
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
Commencez par reproduire l’exemple TypeScript 3.7.5 avec optional chaining et son type de retour string explicite. Déterminez si la correction prévue consiste à inclure undefined dans le type résolu ou à refuser optional chaining sur des valeurs non nullables ; le travail est considéré comme terminé lorsque le comportement choisi est appliqué de manière cohérente et couvert par des tests du compilateur.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100