-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dissallow same parameter signature in overloads #13612
Comments
There is something else involving generics: function sayHi<T extends string>(): T;
function sayHi<T extends object>(): keyof T;
function sayHi(): string { ... } it can match conditionally by type even without using inference: sayHi<'a'>(); // 'a'
sayHi<{b: number}>(); // 'b' If the compiler should enforce that it should keep this behavior intact. Generic constraints contribute identity to the signature. In other words it should only complain when it sees indistinguishable overloads (overloads ONLY different in their return type). Not having this rule enforced haven't got me into trouble so far though. |
I was thinking about type parameter as well, though didn't give an example on it.
|
I think this one is also required for #6606. Since, the following expression is ambiguous: typeof Greet.sayHi(); |
#13225 is in a similar vein. |
Just for your information, the proposed behavior is implemented in pre TS1.1. But it is removed in later releases. http://stackoverflow.com/questions/25022331/overloads-cannot-differ-only-by-return-type Also, the spec disallows same parameter signature in ambient function declaration. |
Duplicate of #13225 |
I think the below overloads are ambiguous:
Since, we are selecting a return type based on the parameter signature. But, what if the parameter signature is the same on multiple overloads? Nowadays, if the checker cannot find an appropriate overload it takes the first one. I think this is quite an unsafe approach. In the above example, it is clear that
sayHi()
cannot be narrowed to eitherstring
ornumber
based on parameter signature selection, since there is only one parameter signature()
, and thus should have the return typestring | number
only.What I propose, is to error on the overload that has the same parameter signature:
The text was updated successfully, but these errors were encountered: