-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
New type param modifier to allow for partial inference based on signature declaration #53999
Comments
This would be a dream come true, for Redux Toolkit, but also so many other situations where I have to make the decision between "do I write this for inference" or "do I write this for the user to pass something in". |
Looks amazing 😍 |
I can't imagine a library that uses generics heavily that wouldn't benefit from this. |
PR with investigation: link |
@phryneas |
Suggestion
🔍 Search Terms
partial inference modifiers type parameters
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Type inference is a powerful tool but for certain scenarios it suffers from a usability issue - it's all or nothing and doesn't allow for "partial application".
There are two angles here:
When it comes to the first one there is already an open proposal for it here and even a PR implementing it here. Based on reactions below both of those I think it's fair to say that it's a highly requested feature.
However, even if that feature would get its way to the language it would still suffer from some usability issues for the mentioned second use case. The problem is that this feature would require an explicit opt-in by the user to leverage this feature. I think that's an acceptable tradeoff for library authors - we certainly wouldn't be super picky about it if that feature would land.
I think though that this could easily be improved by allowing a special type parameter modifier - like
preferinfer
or justinfer
:Both use cases are very much related but I feel like they are orthogonal as one can exist without the other. This proposal focuses only on the second use case. A draft implementation for the feature can be found here.
📃 Motivating Example
In XState we accept a couple of type arguments but we'd also like to additionally perform some additional inference/validation based on the received
config
argument. It's a JSON-like schema:This would be especially useful for us in combination with the recently introduced
const
type parameters.At the moment we have to resort to this, rather silly, workaround:
Drawbacks of this approach:
types
also become part of the inferredTConfig
TEvent
from locations that shouldn't be used as inference sources. A rogueany
somewhere added to inference candidates might implicitly deopt all locations. We have to useNoInfer
trick to avoid thisAnother workaround for this issue is to change the API, use factory functions to "bind" explicit type parameters to the returned function. This alters the API though and I don't think that's really a suitable workaround when better alternatives could exist.
IIRC, Redux Toolkit also uses a similar pattern in its types to "accept an explicit type" for
TState
. And I distinctly remember that Tanner from Tanstack would give a lot for a feature like this (perhaps this would be more useful for Tanstack Table than Tanstack Query). I'm pretty sure that a ton of OSS library authors would just love, love to see a solution for this to land in some shape.The text was updated successfully, but these errors were encountered: