Skip to content

Commit 39b9d81

Browse files
authored
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
1 parent 2d650cb commit 39b9d81

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

api_guard/dist/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export declare function from<O extends ObservableInput<any>>(input: O, scheduler
138138
export declare function fromEvent<T>(target: HasEventTargetAddRemove<T> | ArrayLike<HasEventTargetAddRemove<T>>, eventName: string): Observable<T>;
139139
export declare function fromEvent<T, R>(target: HasEventTargetAddRemove<T> | ArrayLike<HasEventTargetAddRemove<T>>, eventName: string, resultSelector: (event: T) => R): Observable<R>;
140140
export declare function fromEvent<T>(target: HasEventTargetAddRemove<T> | ArrayLike<HasEventTargetAddRemove<T>>, eventName: string, options: EventListenerOptions): Observable<T>;
141-
export declare function fromEvent<T, R>(target: HasEventTargetAddRemove<T> | ArrayLike<HasEventTargetAddRemove<T>>, eventName: string, options: EventListenerOptions, resultSelector: (event: T) => R): Observable<T>;
141+
export declare function fromEvent<T, R>(target: HasEventTargetAddRemove<T> | ArrayLike<HasEventTargetAddRemove<T>>, eventName: string, options: EventListenerOptions, resultSelector: (event: T) => R): Observable<R>;
142142
export declare function fromEvent(target: NodeStyleEventEmitter | ArrayLike<NodeStyleEventEmitter>, eventName: string): Observable<unknown>;
143143
export declare function fromEvent<T>(target: NodeStyleEventEmitter | ArrayLike<NodeStyleEventEmitter>, eventName: string): Observable<T>;
144144
export declare function fromEvent<R>(target: NodeStyleEventEmitter | ArrayLike<NodeStyleEventEmitter>, eventName: string, resultSelector: (...args: any[]) => R): Observable<R>;

spec-dtslint/observables/fromEvent-spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ it('should support an event target source result selector', () => {
1818
const a = fromEvent(eventTargetSource, "click", () => "clunk"); // $ExpectType Observable<string>
1919
});
2020

21+
it('should support an event target source with options', () => {
22+
const a = fromEvent(eventTargetSource, "click", { once: true }); // $ExpectType Observable<Event>
23+
});
24+
25+
it('should support an event target source with options and result selector', () => {
26+
const a = fromEvent(eventTargetSource, "click", { once: true }, () => "clunk"); // $ExpectType Observable<string>
27+
});
28+
2129
declare const documentSource: HTMLDocument;
2230

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

40+
it('should support a document source with options', () => {
41+
const a = fromEvent(documentSource, "click", { once: true }); // $ExpectType Observable<Event>
42+
});
43+
44+
it('should support a document source with options and result selector', () => {
45+
const a = fromEvent(documentSource, "click", { once: true }, () => "clunk"); // $ExpectType Observable<string>
46+
});
47+
3248
// Pick the parts that will match NodeStyleEventEmitter. If this isn't done, it
3349
// will match JQueryStyleEventEmitter - because of the `on` and `off` methods -
3450
// despite the latter being declared last in the EventTargetLike union.

src/internal/observable/fromEvent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function fromEvent<T, R>(
7676
eventName: string,
7777
options: EventListenerOptions,
7878
resultSelector: (event: T) => R
79-
): Observable<T>;
79+
): Observable<R>;
8080

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

0 commit comments

Comments
 (0)