-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unreachable returns badly break return type inference #55437
Comments
Almost certainly this is due to #26914, though I'd agree that any |
Agreed. The type checker should infer based on the code I wrote - not the code it thinks I should have written. It's doubly unfortunate that type inference here degrades to |
Also related: #51800 When you have unreachable code it trips up the type checker. Solution is to not have unreachable code. |
Actually the problem is the opposite - the code it “thinks you should have written” doesn’t include the unreachable bit (hence why CFA doesn’t work there), but it’s trying to infer a return type from it anyway. If it inferred types only from what it thought you should have written then you’d have the desired behavior already. 😉 |
@MartinJohns I mean I guess you could also say “Solution is to write code that so obviously correct you don’t need a type checker” :-p. Even so, reachability can change due to changes in upstream dependencies, environment, or feature flags. Even if CFA does ignore unreachable code and unreachable |
The real issue here is our treatment of "evolving any" types in unreachable code (support for evolving any types was introduced in TS 2.1, see here for details). When a variable with an evolving any type is referenced in unreachable code, we currently assume it has type |
The behavior related to unreachable code isn't limited to auto types. Similar-ish thing happens to variables with declared types: // inferred: function f(): string | number | boolean
function f() {
let x: string | number | boolean;
x = 1;
return x;
x = "foo";
return x;
} |
🔎 Search Terms
unreachable any return
🕗 Version & Regression Information
let
⏯ Playground Link
https://www.typescriptlang.org/play?target=7&jsx=0&ts=5.2.0-beta&allowUnreachableCode=true#code/GYVwdgxgLglg9mABMAFASgN6IPTcQBwCc58BTQgGwE9FDThy6ATRAQwGdEwQBbAI3IAoAJAVSURAA8A3FMQBeRAEZZdKCEJIZI3IgBypSRIowwpREzilOYOBNbAG0WuI1IoVMolMM6kUgBcIpIKiABEwHBwYYIAvoKgkLAIiADm6Fi6PoykLBxsYFQiYhIycooqLuqaUtI6eAZGiCZmiADWMBQUnGpuiB5e2X4QgSK9NdrxidDwSAAWGTh4Q8xsnKyFY641KvX6hsam5h1dPdvunuZDpP5BouKIVLI0Farnj3XxQA
💻 Code
🙁 Actual behavior
The types are inferred as:
The case of
f
demonstrates that return types based on assignment to mutable variables can work. The unreachable assignment tox
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 benumber
and an extra return does not change the type ofx
.In the case of
h
, the return type should be unaffected as well. The second return can be inferred to benumber
.🙂 Expected behavior
The types should be inferred as:
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.The text was updated successfully, but these errors were encountered: