microsoft / microsoft/TypeScript

Unreachable returns badly break return type inference

Open
#55,437 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground reproduction and compare the inferred declarations for f, g, and h under allowUnreachableCode. Trace the compiler's return-type inference and unreachable-code handling; done means g and h infer number like f, and the relevant noImplicitAny diagnostic is produced.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.