TypeScript Version: 2.5.2
Code
TS only check the function signature whether is compatible with that overloaded function's signature. If not, TS report Overload signature is not compatible with function implementation.. It actually doesn't check the function's implementation, only the signature's compatibility. In the example below:
class Animal { }
class Dog extends Animal { }
function test(d: Dog): string
function test(d: Animal): number
function test(d: Animal | Dog): number | string {
if (d instanceof Dog) {
return 42
} else {
return '苟'
}
}
If we call test as:
const o = test(new Dog())
o is inferred as the string type, but actually, it's number type, eventually, cause the runtime error.
Expected behavior:
Report the first and second test function doesn't be implemented. The third test function signature pass. TS should be able to check the implementation with these signatures which are overloaded, we have done the type narrowing.
Actual behavior:
no errors
TypeScript Version: 2.5.2
Code
TS only check the function signature whether is compatible with that overloaded function's signature. If not, TS report
Overload signature is not compatible with function implementation.. It actually doesn't check the function's implementation, only the signature's compatibility. In the example below:If we call
testas:ois inferred as the string type, but actually, it's number type, eventually, cause the runtime error.Expected behavior:
Report the first and second test function doesn't be implemented. The third test function signature pass. TS should be able to check the implementation with these signatures which are overloaded, we have done the type narrowing.
Actual behavior:
no errors