π 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.
π 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
fdemonstrates that return types based on assignment to mutable variables can work. The unreachable assignment toxafter return has no effect on the return type.In the case of
g, the return type should be unaffected -xis already inferred to benumberand 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
returnshould not stop type inference.Also, the code should be flagged by the
noImplicitAnyrule.Additional information about the issue
possibly related to #40393
likely related to #26914
Disabling
allowUnreachableCodedoes not alleviate these issues.