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
{{ message }}
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
While working on dojo/examples#56 I found it nearly impossible to correctly type actions.
There is an assumption that the options passed to the do() implementation have an event object, and that object has a target property. That makes sense when actions are used as event listeners but often actions call other actions, passing an arbitrary object.
Furthermore, the do() method is assumed to return a Task which resolves with the (same type as) the target.
Correctly changing the typing of an action requires three types (DoOptions, TargettedEventObject and ActionState). This requires a fair amount of understanding of the action internals.
Typing an action which handles keypress events is nearly impossible since the KeyboardEvent type is not compatible with the TargettedEventObject. It has a target of course (an HTMLElement), but the do() implementation may return an entirely different object.
The default createAction export should create Action<any, any> instances. It should take type arguments, e.g. createAction<any, KeyboardEvent>() should create an action that can handle keyboard events. In the TodoMVC example, a createAction<any, Item>() should create an action that is called with todo items.
We could export other constructors to make it easier to create the current "targeted event" actions.
I'm not sure whether ActionState deserves to be a generic. It's tempting but we can't provide default values for generics, and the more arguments Action takes the harder it is to customize the typing.
The text was updated successfully, but these errors were encountered:
While working on dojo/examples#56 I found it nearly impossible to correctly type actions.
There is an assumption that the options passed to the
do()
implementation have anevent
object, and that object has atarget
property. That makes sense when actions are used as event listeners but often actions call other actions, passing an arbitrary object.Furthermore, the
do()
method is assumed to return a Task which resolves with the (same type as) thetarget
.Correctly changing the typing of an action requires three types (
DoOptions
,TargettedEventObject
andActionState
). This requires a fair amount of understanding of the action internals.Typing an action which handles keypress events is nearly impossible since the
KeyboardEvent
type is not compatible with theTargettedEventObject
. It has a target of course (anHTMLElement
), but thedo()
implementation may return an entirely different object.I'd like to see an interface closer to this:
The default
createAction
export should createAction<any, any>
instances. It should take type arguments, e.g.createAction<any, KeyboardEvent>()
should create an action that can handle keyboard events. In the TodoMVC example, acreateAction<any, Item>()
should create an action that is called with todo items.We could export other constructors to make it easier to create the current "targeted event" actions.
I'm not sure whether
ActionState
deserves to be a generic. It's tempting but we can't provide default values for generics, and the more argumentsAction
takes the harder it is to customize the typing.The text was updated successfully, but these errors were encountered: