microsoft / microsoft/TypeScript
Unreachable returns badly break return type inference
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 Search Terms
unreachable any return
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
let
⏯ Playground Link
💻 Code
function f(){ // properly referred as number
let x; x = 1; return x;
// Next line does not affect return type inference:
x = "foo"
}
function g(){ // inferred as any
let x; x = 1; return x;
// Next line kills return type inference:
return x;
}
function h(){ // inferred as any
return 1;
// Next line kills return type inference:
let y; y = 1; return y;
}
🙁 Actual behavior
The types are inferred as:
declare function f(): number;
declare function g(): any;
declare function h(): any;
The case of f demonstrates that return types based on assignment to mutable variables can work. The unreachable assignment to x after return has no effect on the return type.
In the case of g, the return type should be unaffected - x is already inferred to be number and an extra return does not change the type of x.
In the case of h, the return type should be unaffected as well. The second return can be inferred to be number.
🙂 Expected behavior
The types should be inferred as:
declare function f(): number;
declare function g(): number;
declare function h(): number;
Either unreachable return statements should not affect return type or return should not stop type inference.
Also, the code should be flagged by the noImplicitAny rule.
Additional information about the issue
possibly related to #40393
likely related to #26914
Disabling allowUnreachableCode does not alleviate these issues.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der verlinkten TypeScript Playground-Reproduktion und vergleiche die inferierten Deklarationen für f, g und h unter allowUnreachableCode. Verfolge die Inferenz des Rückgabetyps des Compilers und die Behandlung von unerreichbarem Code; abgeschlossen ist die Aufgabe, wenn g und h wie f den Typ number inferieren und die relevante noImplicitAny-Diagnose ausgegeben wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100