microsoft / microsoft/TypeScript

Unreachable returns badly break return type inference

Aperta
#55,437 7 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 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

https://www.typescriptlang.org/play?target=7&jsx=0&ts=5.2.0-beta&allowUnreachableCode=true#code/GYVwdgxgLglg9mABMAFASgN6IPTcQBwCc58BTQgGwE9FDThy6ATRAQwGdEwQBbAI3IAoAJAVSURAA8A3FMQBeRAEZZdKCEJIZI3IgBypSRIowwpREzilOYOBNbAG0WuI1IoVMolMM6kUgBcIpIKiABEwHBwYYIAvoKgkLAIiADm6Fi6PoykLBxsYFQiYhIycooqLuqaUtI6eAZGiCZmiADWMBQUnGpuiB5e2X4QgSK9NdrxidDwSAAWGTh4Q8xsnKyFY641KvX6hsam5h1dPdvunuZDpP5BouKIVLI0Farnj3XxQA

💻 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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione collegata in TypeScript Playground e confronta le dichiarazioni inferite per f, g e h con allowUnreachableCode. Traccia l'inferenza del tipo restituito dal compilatore e la gestione del codice irraggiungibile; il lavoro è completo quando g e h inferiscono number come f e viene prodotto il diagnostico noImplicitAny pertinente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.