Allow use of infer in extends clause of generic type parameter #29378
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
Search Terms
generic type parameter infer keyword conditional
Suggestion
Allow the use
infer
keyword in the extends clause of generic type parameters. This would simply be a nice quality of life improvement to avoid unnecessary conditional types or extra free type parameters.Use Cases
Currently if you have a generic type whose parameter is constrained by a generic type, you need to write a conditional to extract the nested generic type:
This seems unfortunate that we need to write a conditional since only the true branch will ever be satisfied. (Additionally, avoiding unnecessary usages of
any
would be nice.) We could avoid some duplication by dropping the constraint:But passing a non array into this type would move the error to wherever
ArrayType
was used (or perhaps even be silenced) instead of on the type parameter itself where the error actually occurred.We could also do this with an extra type parameter, but this puts the onus on the user of the type to pass a correct value (and they can always just pass
unknown
orany
):Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: