You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionfoo(name: number): number;functionfoo(name: string): string;functionfoo(name: string|number): string|number{if(typeofname==="number"){name;// name is number herereturn"foo"// ¿?}else{name;// name is string herereturn10// ¿?}}letbar=foo("foo");// Return a string but it is a number
Expected behavior:
Error on return "foo" and return 10 becasue return types does not match with their signatures.
Actual behavior:
No errors.
Reading specs we can find this:
Thus, using overloads it is possible to statically describe the manner in which a function's return type varies based on its arguments.
The text was updated successfully, but these errors were encountered:
xLama
changed the title
Function's return type does not varies based on its arguments.
Function's return type does not varies based on its arguments. (with overloads)
Jan 3, 2017
Right now there's no verification between any overload signatures and the implementation itself. Check out #13235 which requests this sort of functionality.
TypeScript Version: 2.1.14
Code
Expected behavior:
Error on return "foo" and return 10 becasue return types does not match with their signatures.
Actual behavior:
No errors.
Reading specs we can find this:
Thus, using overloads it is possible to statically describe the manner in which a function's return type varies based on its arguments.
https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#6.2
Worked as intented, maybe?
The text was updated successfully, but these errors were encountered: