Skip to content

Commit

Permalink
fix(fromEvent): Types now properly infer when resultSelector is provi…
Browse files Browse the repository at this point in the history
…ded (#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
  • Loading branch information
rocwang committed Jun 21, 2021
1 parent 2d650cb commit 39b9d81
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api_guard/dist/types/index.d.ts
Expand Up @@ -138,7 +138,7 @@ export declare function from<O extends ObservableInput<any>>(input: O, scheduler
export declare function fromEvent<T>(target: HasEventTargetAddRemove<T> | ArrayLike<HasEventTargetAddRemove<T>>, eventName: string): Observable<T>;
export declare function fromEvent<T, R>(target: HasEventTargetAddRemove<T> | ArrayLike<HasEventTargetAddRemove<T>>, eventName: string, resultSelector: (event: T) => R): Observable<R>;
export declare function fromEvent<T>(target: HasEventTargetAddRemove<T> | ArrayLike<HasEventTargetAddRemove<T>>, eventName: string, options: EventListenerOptions): Observable<T>;
export declare function fromEvent<T, R>(target: HasEventTargetAddRemove<T> | ArrayLike<HasEventTargetAddRemove<T>>, eventName: string, options: EventListenerOptions, resultSelector: (event: T) => R): Observable<T>;
export declare function fromEvent<T, R>(target: HasEventTargetAddRemove<T> | ArrayLike<HasEventTargetAddRemove<T>>, eventName: string, options: EventListenerOptions, resultSelector: (event: T) => R): Observable<R>;
export declare function fromEvent(target: NodeStyleEventEmitter | ArrayLike<NodeStyleEventEmitter>, eventName: string): Observable<unknown>;
export declare function fromEvent<T>(target: NodeStyleEventEmitter | ArrayLike<NodeStyleEventEmitter>, eventName: string): Observable<T>;
export declare function fromEvent<R>(target: NodeStyleEventEmitter | ArrayLike<NodeStyleEventEmitter>, eventName: string, resultSelector: (...args: any[]) => R): Observable<R>;
Expand Down
16 changes: 16 additions & 0 deletions spec-dtslint/observables/fromEvent-spec.ts
Expand Up @@ -18,6 +18,14 @@ it('should support an event target source result selector', () => {
const a = fromEvent(eventTargetSource, "click", () => "clunk"); // $ExpectType Observable<string>
});

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

it('should support an event target source with options and result selector', () => {
const a = fromEvent(eventTargetSource, "click", { once: true }, () => "clunk"); // $ExpectType Observable<string>
});

declare const documentSource: HTMLDocument;

it('should support a document source', () => {
Expand All @@ -29,6 +37,14 @@ it('should support a document source result selector', () => {
const a = fromEvent(documentSource, "click", () => "clunk"); // $ExpectType Observable<string>
});

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

it('should support a document source with options and result selector', () => {
const a = fromEvent(documentSource, "click", { once: true }, () => "clunk"); // $ExpectType Observable<string>
});

// 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.
Expand Down
2 changes: 1 addition & 1 deletion src/internal/observable/fromEvent.ts
Expand Up @@ -76,7 +76,7 @@ export function fromEvent<T, R>(
eventName: string,
options: EventListenerOptions,
resultSelector: (event: T) => R
): Observable<T>;
): Observable<R>;

export function fromEvent(target: NodeStyleEventEmitter | ArrayLike<NodeStyleEventEmitter>, eventName: string): Observable<unknown>;
/** @deprecated Do not specify explicit type parameters. Signatures with type parameters that cannot be inferred will be removed in v8. */
Expand Down

0 comments on commit 39b9d81

Please sign in to comment.