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() type with resultSelector #6447

Merged
merged 4 commits into from Jun 21, 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
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