Closed kitsonk closed 8 years ago
@kitsonk do you think something more like below be more suitable? I'd like to keep the typing for the target but it not just be assumed as this
?
on<T>(type: string, listener: EventedListenerOrArray<T, EventTargettedObject<T>>): Handle;
it is currently
on(type: string, listener: EventedListenerOrArray<this, EventTargettedObject<this>>): Handle;
That still wouldn't work for DOM events, because the target will never be a widget in those events, it will be a DOM node. We could have a TargettedEventedListenerOrArray<T, E>
and an EventedListenerrArray<E>
and make "root" on be TargettedEventedListenerOrArray<T, E> | EventedListenerrArray<E>
and then each of the overloads could choose if they are targetted or not, like:
on(type: 'state:completed', listener: TargettedEventedListenerArray<this, EventTargettedObject<this>>): Handle;
on(type: 'click', listener: EventedListenerOrArray<MouseEvent>): Handle;
It certainly did work when I tried it locally, for dom events I passed any
as the target type
It certainly did work when I tried it locally, for dom events I passed
any
as the target type
Hmmm... What about if you passed HTMLElement
then?
@kitsonk you can use EventTarget
but not HTMLElement
That sounds like a winner!
Currently
Evented
is constrained to only accept anEventTargettedObject
being passed to an event listener inEvented#on
, thoughEvented#emit
does not constrain it the same way.Even though ideally most events on
Evented
should beEventTargettedObject
, in situations where other events, or foreign events (like DOM events) are relayed, theevent.target
may not exist, or may not be constrained to the same type of the emitter.