-
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
Infer Types for Generic Type Parameters That Are Not Specified #43119
Comments
Duplicate of #26349. Partial type inference is on the roadmap under the "Future" section. |
Man. All that searching and still couldn't find that thing. Thanks @MartinJohns! |
Wait @MartinJohns are you sure about that? This situation is a bit different. The ReactJSS library exports functions that the user can use. The (proposed) type parameters are < Ideally, the user could do something like Writing this out now, I guess an alternative is for the library to rearrange props to something like < Thoughts? I'd also like to hear the thoughts of some others briefly. It could be that this is just the trade-off libraries need to take: First, default parameters would need to be defined last. Next, any potential "implicit default params" would need to be put before all the other default params. |
How would you differentiate between inferred type arguments and default type arguments? The idea the TypeScript team plays with is the |
Ah. So you're saying other devs may want all the defaults to appear when at least one type is supplied? And that in that case differentiating between |
Seems this has been officially marked as duplicate with no one interested in commenting further. Probably makes the most sense to close. I hope #26349 gets merged soon. |
Suggestion
Infer types for generic type parameters that are not specified.
🔍 Search Terms
Search Inputs:
Search Time: 21:01.23 (21 minutes)
Potentially related issues (in order of interest):
Though related, I couldn't tell if these issues were exactly the same since the scope here is more narrow.
✅ Viability Checklist
My suggestion meets these guidelines:
The third one threw me off a little. But since the suggestion bases itself on existing TS features, I think this should be safe.
⭐ Suggestion
This is a suggestion to infer types for generic type parameters that are not specified. As an example for clarity, you can play with the code below on the typescript playground:
In the above example, you'll noticed that when no type parameters are provided, TypeScript is able to correctly infer the types on its own. This thankfully includes the property names of the passed object.
However, when any type parameters are provided, TS assumes the default values for all of the remaining type parameters. This actually causes information to be lost, even though the situation hasn't changed. That is, in both situations, the user never explicitly specified the type of the keys,
K
.Instead of assuming the default values for the remaining parameters whenever any types are specified, TS should continue to infer types for the remaining type parameters. The current functionality makes me assume there's some algorithm that tries to infer the types when no type parameters are specified, and it's able to infer object keys. I'd imagine that same algorithm can simply be taken and used to "move down the list of type parameters", so to speak. So it would look for any remaining unspecified types and infer them instead of applying the default values.
📃 Motivating Example
Improved Type Inference for Generics
Previously, when working with optional generic type parameters, TypeScript would assume the default values for any types that the user failed to specify. The issue only occurred if the user specified some of the type parameters. This caused information to be lost (particularly for objects) if the user wanted to specify only a subset of the available types.
Now, TypeScript correctly infers the types for type parameters that are not explicitly provided by the user. The default value is only used if one cannot be determined.
💻 Use Cases
Currently, packages like React JSS rely on TS to improve the user experience. In the case of React JSS, a user supplies an object of key-value pairs, where the values represent CSS styles for components. Users have to keep track of the keys because they're used to identify the different styles. See the React JSS playground for a clear example.
As you'll see in the playground, the class names can be inferred for an improved experience. However, after receiving a request for improved prop-type recognition (cssinjs/jss#1273), the library needs to include generic types. Once generics are introduced, users will get type inference for React prop types but will then lose automated inference for object keys, recreating the issue of poor intellisense.
The only workaround for this issue is to supply a list of strings (
"A" | "B" | "C"
) as a type parameter for the class names. But this can get long with a larger number of style declarations, and it's redundant.The text was updated successfully, but these errors were encountered: