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
Many times you use external/3rd party definition files where one interface contains method that accepts or returns any type.
You know that in your codebase you could provide more specific overload that would help provide useful typings for you code. The problem is overload from external definitions file is always chosen first. The only way to augment this interface is to edit external definition file.
Suggestion
Choose overload with more specific (non-any) parameters when there are two overloads, one with any type and other with concrete or generic type.
Examples
These examples show that order in which interfaces are added matter. These are in one file but in real-life code the any variant is in external definition file.
But the whole point is: The order here should not matter for these cases. TS compiler should always pick more specific overload.
It works for literal types
Actually I read some issue (like #1860) where it was described that order of overloads matter. But I noticed that overloads with literal types are always chosen even when there is matching any overload:
Can't we have similar resolution in "any" vs "non-any" conflicts like in "any" vs "string literal" types? It really would be helpful for external types augmentation scenarios.
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript / JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. new expression-level syntax)
The text was updated successfully, but these errors were encountered:
We're very committed to simple overload resolution - we've experimented with other options and they all had downsides we don't like. In particular, C#'s overly complex overload resolution algorithm is a constant source of pain for them. The string literal choosing thing is bad and we'd prefer to get rid of it instead of spreading that behavior further.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Use Cases
Many times you use external/3rd party definition files where one interface contains method that accepts or returns
any
type.You know that in your codebase you could provide more specific overload that would help provide useful typings for you code. The problem is overload from external definitions file is always chosen first. The only way to augment this interface is to edit external definition file.
Suggestion
Choose overload with more specific (non-
any
) parameters when there are two overloads, one withany
type and other with concrete or generic type.Examples
These examples show that order in which interfaces are added matter. These are in one file but in real-life code the
any
variant is in external definition file.Expected result:
Error in both cases because non-
any
overload was taken firstOther example:
Expected result:
Error in both cases because non-
any
overload was taken firstActually in the cases I shown I thought that when you first declare interface with any then this overload will be taken, just like here:
But the whole point is: The order here should not matter for these cases. TS compiler should always pick more specific overload.
It works for literal types
Actually I read some issue (like #1860) where it was described that order of overloads matter. But I noticed that overloads with literal types are always chosen even when there is matching
any
overload:Can't we have similar resolution in "any" vs "non-any" conflicts like in "any" vs "string literal" types? It really would be helpful for external types augmentation scenarios.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: