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
When a function has several generic types and only one is specified, it seems like the following ones are assigned their default types rather than applying inference. This means that needing to specify the first type forces to specify them all to avoid loosing the benefits of the more precise inferred type for the second generic type.
It would be great that when specifying only partially the generic types for a call, the remaining ones could still benefit from type inference rather than always using their base type.
Use Cases
The use case comes from fregante/delegate-it#19 where I try to make the library nicer for type inference, but an expected usage for TS users would be to explicitly specify the first generic type (for which there is no smart inference possible) and let type inference do its work for the second one.
In the first case, the event is properly inferred as using KeyboardEvent thanks to the 'keydown' event type. That's where type inference shines. But we get Element as the type for the field variable because type inference cannot infer a better type there.
That's where the maintainer of the library would write code using the second syntax, where the generic type for the delegate target is specified explicitly. But doing that looses the inference of KeyboardEvent based on the 'keydown' event type, going back to KeyboardEvent | UIEvent | Event | AnimationEvent | MouseEvent | ... for the event. This happens because it keeps using keyof GlobalEventHandlersEventMap as the TEventType generic rather than inferring it based on the 'keydown' argument.
This would force writing the third ugly syntax (repeating 'keydown') to get both a precise type for field and for event
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. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
Search Terms
generic inference
Suggestion
When a function has several generic types and only one is specified, it seems like the following ones are assigned their default types rather than applying inference. This means that needing to specify the first type forces to specify them all to avoid loosing the benefits of the more precise inferred type for the second generic type.
It would be great that when specifying only partially the generic types for a call, the remaining ones could still benefit from type inference rather than always using their base type.
Use Cases
The use case comes from fregante/delegate-it#19 where I try to make the library nicer for type inference, but an expected usage for TS users would be to explicitly specify the first generic type (for which there is no smart inference possible) and let type inference do its work for the second one.
Examples
I'm reproducing here the types from the PR above:
Here is 3 different usages:
In the first case, the event is properly inferred as using
KeyboardEvent
thanks to the'keydown'
event type. That's where type inference shines. But we getElement
as the type for thefield
variable because type inference cannot infer a better type there.That's where the maintainer of the library would write code using the second syntax, where the generic type for the delegate target is specified explicitly. But doing that looses the inference of
KeyboardEvent
based on the'keydown'
event type, going back toKeyboardEvent | UIEvent | Event | AnimationEvent | MouseEvent | ...
for the event. This happens because it keeps usingkeyof GlobalEventHandlersEventMap
as theTEventType
generic rather than inferring it based on the'keydown'
argument.This would force writing the third ugly syntax (repeating
'keydown'
) to get both a precise type forfield
and forevent
Checklist
My suggestion meets these guidelines:
I'm not sure whether changing this would break existing Typescript code.
The text was updated successfully, but these errors were encountered: