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

fix(fromEvent): fixed HasEventTargetAddRemove to support EventTarget types #5945

Merged
merged 1 commit into from Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 19 additions & 13 deletions spec-dtslint/observables/fromEvent-spec.ts
@@ -1,37 +1,43 @@
import { fromEvent } from 'rxjs';
import { JQueryStyleEventEmitter } from '../../src/internal/observable/fromEvent';
import { A, B } from "../helpers";
import { A, B } from '../helpers';

declare const eventTargetSource: HTMLDocument;
declare const eventTargetSource: EventTarget;

it('should support an event target source', () => {
const a = fromEvent(eventTargetSource, "click"); // $ExpectType Observable<Event>
});

declare const documentSource: HTMLDocument;

it('should support a document source', () => {
const a = fromEvent(documentSource, "click"); // $ExpectType Observable<Event>
});

interface NodeStyleSource {
addListener: (eventName: string | symbol, handler: (...args: any[]) => void) => this;
removeListener: (eventName: string | symbol, handler: (...args: any[]) => void) => this;
};
declare const nodeStyleSource : NodeStyleSource;

declare const nodeCompatibleSource: {
addListener: (eventName: string, handler: (...args: any[]) => void) => void;
removeListener: (eventName: string, handler: (...args: any[]) => void) => void;
};



it('should support an event target source', () => {
const a = fromEvent(eventTargetSource, "click"); // $ExpectType Observable<Event>
});
declare const nodeStyleSource : NodeStyleSource;

it('should support a node-style source', () => {
const a = fromEvent(nodeStyleSource, "something"); // $ExpectType Observable<unknown>
const b = fromEvent<B>(nodeStyleSource, "something"); // $ExpectType Observable<B>
});

declare const nodeCompatibleSource: {
addListener: (eventName: string, handler: (...args: any[]) => void) => void;
removeListener: (eventName: string, handler: (...args: any[]) => void) => void;
};

it('should support a node-compatible source', () => {
const a = fromEvent(nodeCompatibleSource, "something"); // $ExpectType Observable<unknown>
const b = fromEvent<B>(nodeCompatibleSource, "something"); // $ExpectType Observable<B>
});

declare const jQueryStyleSource: JQueryStyleEventEmitter<A, B>;

it('should support a jQuery-style source', () => {
const a = fromEvent(jQueryStyleSource, "something"); // $ExpectType Observable<B>
const b = fromEvent<B>(jQueryStyleSource, "something"); // $ExpectType Observable<B>
Expand Down
16 changes: 14 additions & 2 deletions src/internal/observable/fromEvent.ts
Expand Up @@ -33,9 +33,21 @@ export interface JQueryStyleEventEmitter<TContext, T> {
off: (eventName: string, handler: (this: TContext, t: T, ...args: any[]) => any) => void;
}

export interface EventListenerObject<E> {
handleEvent(evt: E): void;
}

export interface HasEventTargetAddRemove<E> {
addEventListener(type: string, listener: ((evt: E) => void) | null, options?: boolean | AddEventListenerOptions): void;
removeEventListener(type: string, listener?: ((evt: E) => void) | null, options?: EventListenerOptions | boolean): void;
addEventListener(
type: string,
listener: ((evt: E) => void) | EventListenerObject<E> | null,
options?: boolean | AddEventListenerOptions
): void;
removeEventListener(
type: string,
listener: ((evt: E) => void) | EventListenerObject<E> | null,
options?: EventListenerOptions | boolean
): void;
}

export type EventTargetLike<T> =
Expand Down