From 39b9d818ef6ea033dc8e53800e3a220d56c76b4a Mon Sep 17 00:00:00 2001 From: Roc Wong Date: Tue, 22 Jun 2021 02:39:47 +1200 Subject: [PATCH] fix(fromEvent): Types now properly infer when resultSelector is provided (#6447) * fix: fromEvent() type with resultSelector * chore: api_guardian update * test: improve the dtslint test on fromEvent() * test: fix a typo in the dtslint test for fromEvent --- api_guard/dist/types/index.d.ts | 2 +- spec-dtslint/observables/fromEvent-spec.ts | 16 ++++++++++++++++ src/internal/observable/fromEvent.ts | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/api_guard/dist/types/index.d.ts b/api_guard/dist/types/index.d.ts index 4c34cc78a3..0bd40f38b6 100644 --- a/api_guard/dist/types/index.d.ts +++ b/api_guard/dist/types/index.d.ts @@ -138,7 +138,7 @@ export declare function from>(input: O, scheduler export declare function fromEvent(target: HasEventTargetAddRemove | ArrayLike>, eventName: string): Observable; export declare function fromEvent(target: HasEventTargetAddRemove | ArrayLike>, eventName: string, resultSelector: (event: T) => R): Observable; export declare function fromEvent(target: HasEventTargetAddRemove | ArrayLike>, eventName: string, options: EventListenerOptions): Observable; -export declare function fromEvent(target: HasEventTargetAddRemove | ArrayLike>, eventName: string, options: EventListenerOptions, resultSelector: (event: T) => R): Observable; +export declare function fromEvent(target: HasEventTargetAddRemove | ArrayLike>, eventName: string, options: EventListenerOptions, resultSelector: (event: T) => R): Observable; export declare function fromEvent(target: NodeStyleEventEmitter | ArrayLike, eventName: string): Observable; export declare function fromEvent(target: NodeStyleEventEmitter | ArrayLike, eventName: string): Observable; export declare function fromEvent(target: NodeStyleEventEmitter | ArrayLike, eventName: string, resultSelector: (...args: any[]) => R): Observable; diff --git a/spec-dtslint/observables/fromEvent-spec.ts b/spec-dtslint/observables/fromEvent-spec.ts index 705c55a9bb..8ef58e387c 100644 --- a/spec-dtslint/observables/fromEvent-spec.ts +++ b/spec-dtslint/observables/fromEvent-spec.ts @@ -18,6 +18,14 @@ it('should support an event target source result selector', () => { const a = fromEvent(eventTargetSource, "click", () => "clunk"); // $ExpectType Observable }); +it('should support an event target source with options', () => { + const a = fromEvent(eventTargetSource, "click", { once: true }); // $ExpectType Observable +}); + +it('should support an event target source with options and result selector', () => { + const a = fromEvent(eventTargetSource, "click", { once: true }, () => "clunk"); // $ExpectType Observable +}); + declare const documentSource: HTMLDocument; it('should support a document source', () => { @@ -29,6 +37,14 @@ it('should support a document source result selector', () => { const a = fromEvent(documentSource, "click", () => "clunk"); // $ExpectType Observable }); +it('should support a document source with options', () => { + const a = fromEvent(documentSource, "click", { once: true }); // $ExpectType Observable +}); + +it('should support a document source with options and result selector', () => { + const a = fromEvent(documentSource, "click", { once: true }, () => "clunk"); // $ExpectType Observable +}); + // Pick the parts that will match NodeStyleEventEmitter. If this isn't done, it // will match JQueryStyleEventEmitter - because of the `on` and `off` methods - // despite the latter being declared last in the EventTargetLike union. diff --git a/src/internal/observable/fromEvent.ts b/src/internal/observable/fromEvent.ts index a027664e06..59dae2a2c5 100644 --- a/src/internal/observable/fromEvent.ts +++ b/src/internal/observable/fromEvent.ts @@ -76,7 +76,7 @@ export function fromEvent( eventName: string, options: EventListenerOptions, resultSelector: (event: T) => R -): Observable; +): Observable; export function fromEvent(target: NodeStyleEventEmitter | ArrayLike, eventName: string): Observable; /** @deprecated Do not specify explicit type parameters. Signatures with type parameters that cannot be inferred will be removed in v8. */