Skip to content

Commit

Permalink
feat(esl-utils): make DelegatedEvent equal too `DelegatedEvent<Event>…
Browse files Browse the repository at this point in the history
…`, fix types compatibility (#2360)

Co-authored-by: Feoktist Shovchko <fshovchko@exadel.com>
  • Loading branch information
ala-n and fshovchko authored Apr 19, 2024
1 parent e45bb0f commit 458ea53
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/modules/esl-event-listener/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ declare global {
}

/** Extended event with a delegated event target */
export type DelegatedEvent<EventType extends Event> = EventType & {
export type DelegatedEvent<EventType extends Event = Event> = EventType & {
/** Delegated target element, that exactly accepted by `selector` CSS selector */
$delegate: Element | null;
};

Expand Down
15 changes: 14 additions & 1 deletion src/modules/esl-utils/decorators/listen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ESLEventUtils} from '../../esl-event-listener/core';

import type {PropertyProvider} from '../misc/functions';
import type {ESLListenerHandler, ESLListenerDescriptorExt} from '../dom/events';
import type {DelegatedEvent, ESLListenerHandler, ESLListenerDescriptorExt} from '../../esl-event-listener/core';

type ListenDecorator<EType extends Event> =
(target: any, property: string, descriptor: TypedPropertyDescriptor<ESLListenerHandler<EType>>) => void;
Expand All @@ -12,6 +12,19 @@ type ListenDecorator<EType extends Event> =
* @param event - event type string or event provider function
*/
export function listen<K extends keyof ESLListenerEventMap>(event: K | PropertyProvider<K>): ListenDecorator<ESLListenerEventMap[K]>;
/**
* Decorator to declare listener ({@link ESLEventListener}) meta information using {@link ESLListenerDescriptor}
* Defines auto-subscribable event by default
* @param desc - event listener configuration {@link ESLListenerDescriptor}
*
* Note: you are using delegation as you declare `selector` property in `desc`.
* Consider using {@link DelegatedEvent} event type wrapper in case you need to access `event.$delegate` property
*
* @see DelegatedEvent.prototype.$delegate
*/
export function listen<K extends keyof ESLListenerEventMap>(
desc: ESLListenerDescriptorExt<K> & {selector: string | PropertyProvider<string>}
): ListenDecorator<DelegatedEvent<ESLListenerEventMap[K]>>;
/**
* Decorator to declare listener ({@link ESLEventListener}) meta information using {@link ESLListenerDescriptor}
* Defines auto-subscribable event by default
Expand Down

0 comments on commit 458ea53

Please sign in to comment.