Enable Generics to be inferred on assignment #47755
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Suggestion
This suggestion is to enable Generics to be inferred and defined in a single step. This could look something like this:
The
<*>
indicates that the default type of the[...] as const
should be passed as the first argument toInferredGeneric
andInferredGeneric
's return type used as the final type forx
.Even better would be to combine this with issue: #30680:
🔍 Search Terms
"infer generic type"
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Enhance generics to accept
<*>
or<* as const>
when they are used and this would mean the default type of the variable being defined is passed to the generic and whatever it returns would be the final variable type.📃 Motivating Example
If generics cannot be defined and inferred in a single step, then it breaks the DRY principle as one would have to do the following:
This solution would combine STEP 1 and STEP 2 into a single easy to read and understand step, and so would become:
code
💻 Use Cases
Makes generics more powerful by inferred them and applied the resultant definition in a single step.
The text was updated successfully, but these errors were encountered: