Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed parameter union type. #7

Conversation

nickytonline
Copy link
Collaborator

Fixes #6

@@ -115,7 +115,7 @@ function delegate<TElement extends Element = Element, TEvent extends Event = Eve

// Single base element specified
function delegate<TElement extends Element = Element, TEvent extends Event = Event>(
elements: EventTarget | Document,
elements: EventTarget | Document | HTMLElement,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

select(...) from your example has the following type

declare function select<T extends HTMLElement = HTMLElement>(selectors: string, baseElement?: BaseElement): T | null;

so I simply added HTMLElement to the union type for the elements parameter. I don't think Element, DocumentFragment or Node are required in the union type, but if you want those ones as well, let me know.

Copy link
Owner

@fregante fregante May 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s weird though, this shouldn’t be required, HTMLElement is already a EventTarget, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although an HTMLElement has a subset of its interface which is almost equivalent to EventTarget, it cannot be assigned directly.

From lib.dom.d.ts

/** EventTarget is an interface implemented by objects that can receive events and may have listeners for them. */
interface EventTarget {
    /**
     * Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
     * The options argument sets listener-specific options. For compatibility this can be a
     * boolean, in which case the method behaves exactly as if the value was specified as options's capture.
     * When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
     * When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in §2.8 Observing event listeners.
     * When set to true, options's once indicates that the callback will only be invoked once after which the event listener will
     * be removed.
     * The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
     */
    addEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
    /**
     * Dispatches a synthetic event event to target and returns true
     * if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
     */
    dispatchEvent(event: Event): boolean;
    /**
     * Removes the event listener in target's event listener list with the same type, callback, and options.
     */
    removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
}

...

/** The HTMLElement interface represents any HTML element. Some elements directly implement this interface, others implement it via an interface that inherits it. */
interface HTMLElement extends Element, GlobalEventHandlers, DocumentAndElementEventHandlers, ElementContentEditable, HTMLOrSVGElement, ElementCSSInlineStyle {
    accessKey: string;
    readonly accessKeyLabel: string;
    autocapitalize: string;
    dir: string;
    draggable: boolean;
    hidden: boolean;
    innerText: string;
    lang: string;
    readonly offsetHeight: number;
    readonly offsetLeft: number;
    readonly offsetParent: Element | null;
    readonly offsetTop: number;
    readonly offsetWidth: number;
    spellcheck: boolean;
    title: string;
    translate: boolean;
    click(): void;
    addEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
    addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
    removeEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
    removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
}

Which results in Type 'HTMLElement' is not assignable to type 'EventTarget'..

Copy link
Owner

@fregante fregante May 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes I get lost in the "direction" of types (e.g. assign Element to a node variable and viceversa) but this seems straightforward:

HTMLElement extends Element extends Node extends EventTarget

If delegate accepts EventTarget it should accept anything that extends it, right?


https://www.typescriptlang.org/play/#src=let%20target%3A%20EventTarget%3B%0D%0A%0D%0Atarget%20%3D%20null%20as%20unknown%20as%20HTMLElement%3B

let target: EventTarget;
target = null as unknown as HTMLElement; // Even passes `strict`

So HTMLElement should be assignable to type EventTarget


_delegate's own first parameter type is EventTarget, everything goes through that. I'm thinking it's a TS overload bug

Copy link
Owner

@fregante fregante May 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.typescriptlang.org/play/#src=let%20target%3A%20EventTarget%3B%0D%0A%0D%0Atarget%20%3D%20null%20as%20unknown%20as%20HTMLElement%3B

let target: EventTarget;
target = null as unknown as HTMLElement; // Even passes `strict`

So HTMLElement should be assignable to type EventTarget

@fregante
Copy link
Owner

fregante commented May 9, 2019

#6 (comment)

@fregante fregante closed this May 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type of delegate(Element, string, EventType, Handler) doesn't seem to work
2 participants