Skip to content

Commit

Permalink
fix(types): support union types passed to defer
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Jan 9, 2019
1 parent e2878af commit 5aea50e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
6 changes: 5 additions & 1 deletion spec-dtslint/observables/defer-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import { of, defer } from 'rxjs';
it('should enforce function parameter', () => {
const a = defer(); // $ExpectError
});

it('should infer correctly with function return observable', () => {
const a = defer(() => of(1, 2, 3)); // $ExpectType Observable<number>
});

it('should infer correctly with function return promise', () => {
const a = defer(() => Promise.resolve(5)); // $ExpectType Observable<number>
});

it('should support union type returns', () => {
const a = defer(() => Math.random() > 0.5 ? of(123) : of('abc')); // $ExpectType Observable<string | number>
});
10 changes: 5 additions & 5 deletions src/internal/observable/defer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Observable } from '../Observable';
import { SubscribableOrPromise } from '../types';
import { SubscribableOrPromise, ObservedValueOf, ObservableInput } from '../types';
import { from } from './from'; // lol
import { empty } from './empty';

Expand Down Expand Up @@ -50,9 +50,9 @@ import { empty } from './empty';
* @name defer
* @owner Observable
*/
export function defer<T>(observableFactory: () => SubscribableOrPromise<T> | void): Observable<T> {
return new Observable(subscriber => {
let input: SubscribableOrPromise<T> | void;
export function defer<O extends ObservableInput<any>>(observableFactory: () => O | void): Observable<ObservedValueOf<O>> {
return new Observable<ObservedValueOf<O>>(subscriber => {
let input: O | void;
try {
input = observableFactory();
} catch (err) {
Expand All @@ -62,4 +62,4 @@ export function defer<T>(observableFactory: () => SubscribableOrPromise<T> | voi
const source = input ? from(input) : empty();
return source.subscribe(subscriber);
});
}
}
2 changes: 1 addition & 1 deletion src/internal/observable/iif.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@ export function iif<T, F>(
trueResult: SubscribableOrPromise<T> = EMPTY,
falseResult: SubscribableOrPromise<F> = EMPTY
): Observable<T|F> {
return defer<T|F>(() => condition() ? trueResult : falseResult);
return defer(() => condition() ? trueResult : falseResult);
}

0 comments on commit 5aea50e

Please sign in to comment.