-
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
TS doesn't check the implementation of the overloaded function whether match its signature. #18533
Comments
The check is only to ensure that the signatures are sufficiently compatible. The overloads are a promise you're making to API consumers, while TypeScript allows you to be as flexible as possible to fulfill each of those signatures in the implementation. Unfortunately we don't really have any simple ideas about how to perform this check efficiently. |
In case OP wonder why the check is prohibitively expensive: It's TypeScript's design limitation. TS has a structural typing system, that is, the assignability between two types relies solely on their property types. (e.g. your example function In order to check if a branch return value is compatible to the signature, the compiler will have to check all signatures from top to bottom until one signature matches. The complexity is about O(mn) where m is the number of branches and n is the number of signatures. For nontrivial overload functions, this complexity will easily trigger out of memory error or too time consuming. |
What if adding some annotations which are required on branches? We manually tell TS this branch match which signature. Inside the function, TS just check the narrowed type and the return type. Outside, TS check if all the signature functions have their annotations in the function implementation? Just need a Map with signature and annotation pairs. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Is there any plan for breaking this limitation? |
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
test
as: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
The text was updated successfully, but these errors were encountered: