microsoft / microsoft/TypeScript

Incorporating exhaustiveness analysis into the control flow of if statements.

Offen
#56,527 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: check: Control Flow Possible Improvement
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔍 Search Terms

"initialization", "variable", "inference", "assignment", "assigned", "automatic detection"

✅ Viability Checklist
⭐ 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
  1. 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.
  2. 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.
  3. What workarounds are you using in the meantime?
    Replacing the if-else clause with a switch statement.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das Dinosaurier-Beispiel aus dem Issue nachzubilden, und vergleiche sein Verhalten bei der definite-assignment-Analyse mit dem der äquivalenten switch-Anweisung. Verfolge die Control-Flow- und Exhaustiveness-Analyse des Compilers, um zu bestimmen, wo if-else-Zweige behandelt werden; die Aufgabe ist abgeschlossen, wenn exhaustive Zweige nicht mehr den used-before-assigned-Fehler erzeugen, ohne das erzeugte JavaScript zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.