Skip to content
Permalink
Browse files

fix(TS): fix type inference for defaultIfEmpty. (#4833)

  • Loading branch information...
bowenni authored and benlesh committed Jun 4, 2019
1 parent f31c3df commit 9b5ce2fada18dab421f15d521426695d24c08fda
@@ -1,5 +1,5 @@
import { of } from 'rxjs';
import { defaultIfEmpty } from 'rxjs/operators';
import { defaultIfEmpty, map } from 'rxjs/operators';

it('should infer correctly', () => {
const o = of(1, 2, 3).pipe(defaultIfEmpty()); // $ExpectType Observable<number>
@@ -13,6 +13,10 @@ it('should infer correctly with a different type of defaultValue', () => {
const o = of(1, 2, 3).pipe(defaultIfEmpty<number, string>('carbonara')); // $ExpectType Observable<string | number>
});

it('should infer correctly with a subtype passed through parameters', () => {
const o = of(true, false).pipe(map(p => p), defaultIfEmpty(true)); // $ExpectType Observable<boolean>
});

it('should enforce types', () => {
const o = of(1, 2, 3).pipe(defaultIfEmpty(4, 5)); // $ExpectError
});
@@ -4,8 +4,7 @@ import { Subscriber } from '../Subscriber';
import { OperatorFunction, MonoTypeOperatorFunction } from '../types';

/* tslint:disable:max-line-length */
export function defaultIfEmpty<T>(defaultValue?: T): MonoTypeOperatorFunction<T>;
export function defaultIfEmpty<T, R>(defaultValue?: R): OperatorFunction<T, T | R>;
export function defaultIfEmpty<T, R = T>(defaultValue?: R): OperatorFunction<T, T | R>;
/* tslint:enable:max-line-length */

/**
@@ -84,6 +84,6 @@ export function first<T, D>(
return (source: Observable<T>) => source.pipe(
predicate ? filter((v, i) => predicate(v, i, source)) : identity,
take(1),
hasDefaultValue ? defaultIfEmpty<T | D>(defaultValue) : throwIfEmpty(() => new EmptyError()),
hasDefaultValue ? defaultIfEmpty<T, D>(defaultValue) : throwIfEmpty(() => new EmptyError()),
);
}
@@ -47,6 +47,6 @@ export function last<T, D>(
return (source: Observable<T>) => source.pipe(
predicate ? filter((v, i) => predicate(v, i, source)) : identity,
takeLast(1),
hasDefaultValue ? defaultIfEmpty<T | D>(defaultValue) : throwIfEmpty(() => new EmptyError()),
hasDefaultValue ? defaultIfEmpty<T, D>(defaultValue) : throwIfEmpty(() => new EmptyError()),
);
}

0 comments on commit 9b5ce2f

Please sign in to comment.
You can’t perform that action at this time.