Closed
Description
TypeScript Version: 2.9.0-dev.20180503
Search Terms: overload, function, parameter, return
Code
function funct5(param1: boolean): string;
function funct5(param1: Date): number;
function funct5(param1: boolean | Date): string | number {
if (typeof param1 === "boolean") {
return 1; // Expected to have a TS error saying that only a string is valid
} else {
return 0;
}
}
Expected behavior:
Since the param1 is narrowed down to be a boolean, only a single overload is valid (the first one that returns a string). However, I can return a number which should only be valid if the param1 is a Date.
Actual behavior:
I was expecting TypeScript to notify me that the return type was invalid because of the type of the param1.
Playground Link: Link
Related Issues: