Description
TypeScript Version: 2.7.2
Search Terms: type inference, type narrowing, overloads
Code
function bar(selector: "A", val: number): number;
function bar(selector: "B", val: string): string;
function bar(selector: "A" | "B", val: number | string): number | string
{
if (selector === "A")
{
const the_selector = selector; // "A"
const the_val = val; // number | string but should be just number
return "foo"; // should give error: when selector is "A" should return a number
}
else
{
const the_selector = selector; // "B"
const the_val = val; // number | string but should be just string
return 42; // should give error: when selector is "B" should return a string
}
}
Issue
The snippet above shows a limitation of the current type inference implementation in TypeScript.
As can be clearly seen from the overloaded declarations, when selector === "A"
we have val: number
and we must return a number
, while when selector === "B"
we have val: string
and we must return a string
. But the type-checker, although able to understand that the_selector
can only be "A"
in the if
block and only "B"
in the else
block, still types the_val
as number | string
in both blocks and allows us to return a string
when selector === "A"
and a number
when selector === "B"
.
Possible Solution
In the implementation of an overloaded function, when the type-checker decides to narrow the type of a parameter (as often happens in if-else
blocks), it should also remove the overloads that don't match the narrowed parameter type from the overload set, and use the pruned overload set to narrow the types of the other parameters.