Skip to content

Commit 5f022d7

Browse files
authored
fix(fromEvent): fixed HasEventTargetAddRemove to support EventTarget types (#5945)
1 parent a85627f commit 5f022d7

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

spec-dtslint/observables/fromEvent-spec.ts

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
import { fromEvent } from 'rxjs';
22
import { JQueryStyleEventEmitter } from '../../src/internal/observable/fromEvent';
3-
import { A, B } from "../helpers";
3+
import { A, B } from '../helpers';
44

5-
declare const eventTargetSource: HTMLDocument;
5+
declare const eventTargetSource: EventTarget;
6+
7+
it('should support an event target source', () => {
8+
const a = fromEvent(eventTargetSource, "click"); // $ExpectType Observable<Event>
9+
});
10+
11+
declare const documentSource: HTMLDocument;
12+
13+
it('should support a document source', () => {
14+
const a = fromEvent(documentSource, "click"); // $ExpectType Observable<Event>
15+
});
616

717
interface NodeStyleSource {
818
addListener: (eventName: string | symbol, handler: (...args: any[]) => void) => this;
919
removeListener: (eventName: string | symbol, handler: (...args: any[]) => void) => this;
1020
};
11-
declare const nodeStyleSource : NodeStyleSource;
12-
13-
declare const nodeCompatibleSource: {
14-
addListener: (eventName: string, handler: (...args: any[]) => void) => void;
15-
removeListener: (eventName: string, handler: (...args: any[]) => void) => void;
16-
};
1721

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

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

29+
declare const nodeCompatibleSource: {
30+
addListener: (eventName: string, handler: (...args: any[]) => void) => void;
31+
removeListener: (eventName: string, handler: (...args: any[]) => void) => void;
32+
};
33+
2934
it('should support a node-compatible source', () => {
3035
const a = fromEvent(nodeCompatibleSource, "something"); // $ExpectType Observable<unknown>
3136
const b = fromEvent<B>(nodeCompatibleSource, "something"); // $ExpectType Observable<B>
3237
});
3338

3439
declare const jQueryStyleSource: JQueryStyleEventEmitter<A, B>;
40+
3541
it('should support a jQuery-style source', () => {
3642
const a = fromEvent(jQueryStyleSource, "something"); // $ExpectType Observable<B>
3743
const b = fromEvent<B>(jQueryStyleSource, "something"); // $ExpectType Observable<B>

src/internal/observable/fromEvent.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,21 @@ export interface JQueryStyleEventEmitter<TContext, T> {
3333
off: (eventName: string, handler: (this: TContext, t: T, ...args: any[]) => any) => void;
3434
}
3535

36+
export interface EventListenerObject<E> {
37+
handleEvent(evt: E): void;
38+
}
39+
3640
export interface HasEventTargetAddRemove<E> {
37-
addEventListener(type: string, listener: ((evt: E) => void) | null, options?: boolean | AddEventListenerOptions): void;
38-
removeEventListener(type: string, listener?: ((evt: E) => void) | null, options?: EventListenerOptions | boolean): void;
41+
addEventListener(
42+
type: string,
43+
listener: ((evt: E) => void) | EventListenerObject<E> | null,
44+
options?: boolean | AddEventListenerOptions
45+
): void;
46+
removeEventListener(
47+
type: string,
48+
listener: ((evt: E) => void) | EventListenerObject<E> | null,
49+
options?: EventListenerOptions | boolean
50+
): void;
3951
}
4052

4153
export type EventTargetLike<T> =

0 commit comments

Comments
 (0)