microsoft / microsoft/TypeScript
Incorporating exhaustiveness analysis into the control flow of if statements.
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
🔍 Search Terms
"initialization", "variable", "inference", "assignment", "assigned", "automatic detection"
✅ Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
One's code may implicitely involve that all cases have been processed in an if-else structure. TS does detect it with switch statements, however it does not detect it with if-else clauses.
Consider the following example:
type Dinosaur = "triceratops" | "brachiosaurus"
const myCollectionOfDinosaurs: Dinosaur[] = ["triceratops", "brachiosaurus"]
const randomKey = Math.floor(Math.random() * myCollectionOfDinosaurs.length)
const randomlyChosenDinosaur: Dinosaur = myCollectionOfDinosaurs[randomKey]
let dinosaurGreeting: string
if (randomlyChosenDinosaur === "triceratops") dinosaurGreeting = "Hello triceratops!"
else if (randomlyChosenDinosaur === "brachiosaurus") dinosaurGreeting = "Hello brachiosaurus!"
console.log(dinosaurGreeting)
TS complains on the last line because it says dinosaurGreeting is used before being assigned.
Replacing the if-else clause with a switch statement fixes the problem. However it would be great if TS did support this feature for if-else clauses too.
📃 Motivating Example
I'd use the dinosaurs example above, and assert that this feature would remove unnecessary constraints in the language.
💻 Use Cases
- What do you want to use this for?
Writing some meaningful code with less constraints, shaping it more precisely and remaining safe from type-related problems. - What shortcomings exist with current approaches?
Most current approaches involve working around the way you intend your code to work. For example I can use !: when typing the variable, or initialize the variable with a default variable, or define it anywhere it is initialized, but all these solutions would imply thinking the code differently, in a way that is less compatible with my need.
The best approach is using a switch statement instead of an if-else clause, which doesn't impact the logic of the code. - What workarounds are you using in the meantime?
Replacing the if-else clause with a switch statement.
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 du dinosaure de l’issue et comparez son comportement d’affectation définitive avec celui de l’instruction switch équivalente. Suivez l’analyse du flux de contrôle et de l’exhaustivité du compilateur afin de déterminer où les branches if-else sont traitées ; le travail est terminé lorsque les branches exhaustives ne produisent plus l’erreur de variable utilisée avant affectation, sans modifier le JavaScript généré.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- 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