Skip to content

Commit 6ef371d

Browse files
committed
feat: add generic type parameter to using for the resource
This overload alleviates the observableFactory from needing to cast its parameter to a more specific type.
1 parent 123a0f2 commit 6ef371d

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

api_guard/dist/types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,8 @@ export interface UnsubscriptionError extends Error {
841841

842842
export declare const UnsubscriptionError: UnsubscriptionErrorCtor;
843843

844-
export declare function using<T extends ObservableInput<any>>(resourceFactory: () => Unsubscribable | void, observableFactory: (resource: Unsubscribable | void) => T | void): Observable<ObservedValueOf<T>>;
844+
export declare function using<T extends ObservableInput<any>, R extends Unsubscribable = Unsubscribable>(resourceFactory: () => R, observableFactory: (resource: R) => T | void): Observable<ObservedValueOf<T>>;
845+
export declare function using<T extends ObservableInput<any>>(resourceFactory: () => void, observableFactory: () => T | void): Observable<ObservedValueOf<T>>;
845846

846847
export declare type ValueFromArray<A extends readonly unknown[]> = A extends Array<infer T> ? T : never;
847848

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { using } from 'rxjs';
1+
import { using, Subscription, of} from 'rxjs';
22
import { a$, b$ } from '../helpers';
33

44
it('should infer with a simple factory', () => {
@@ -7,4 +7,8 @@ it('should infer with a simple factory', () => {
77

88
it('should infer with a factory that returns a union', () => {
99
const o = using(() => {}, () => Math.random() < 0.5 ? a$ : b$); // $ExpectType Observable<A | B>
10-
});
10+
});
11+
12+
it('should infer resource types', () => {
13+
const o = using(() => new Subscription(), (sub) => of(sub)); // $ExpectType Observable<Subscription>
14+
});

src/internal/observable/using.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@ import { EMPTY } from './empty';
3131
* @return {Observable<T>} An Observable that behaves the same as Observable returned by `observableFactory`, but
3232
* which - when completed, errored or unsubscribed - will also call `unsubscribe` on created resource object.
3333
*/
34+
export function using<T extends ObservableInput<any>, R extends Unsubscribable = Unsubscribable>(
35+
resourceFactory: () => R,
36+
observableFactory: (resource: R) => T | void
37+
): Observable<ObservedValueOf<T>>;
3438
export function using<T extends ObservableInput<any>>(
35-
resourceFactory: () => Unsubscribable | void,
36-
observableFactory: (resource: Unsubscribable | void) => T | void
39+
resourceFactory: () => void,
40+
observableFactory: () => T | void
41+
): Observable<ObservedValueOf<T>>;
42+
export function using<T extends ObservableInput<any>, R extends Unsubscribable = Unsubscribable>(
43+
resourceFactory: () => R | void,
44+
observableFactory: (resource: R | void) => T | void
3745
): Observable<ObservedValueOf<T>> {
3846
return new Observable<ObservedValueOf<T>>((subscriber) => {
3947
const resource = resourceFactory();

0 commit comments

Comments
 (0)