From 9b5ce2fada18dab421f15d521426695d24c08fda Mon Sep 17 00:00:00 2001 From: Bowen Ni Date: Tue, 4 Jun 2019 15:29:52 -0700 Subject: [PATCH] fix(TS): fix type inference for defaultIfEmpty. (#4833) --- spec-dtslint/operators/defaultIfEmpty-spec.ts | 6 +++++- src/internal/operators/defaultIfEmpty.ts | 3 +-- src/internal/operators/first.ts | 2 +- src/internal/operators/last.ts | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/spec-dtslint/operators/defaultIfEmpty-spec.ts b/spec-dtslint/operators/defaultIfEmpty-spec.ts index 0279327fe2..84ee0877f0 100644 --- a/spec-dtslint/operators/defaultIfEmpty-spec.ts +++ b/spec-dtslint/operators/defaultIfEmpty-spec.ts @@ -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 @@ -13,6 +13,10 @@ it('should infer correctly with a different type of defaultValue', () => { const o = of(1, 2, 3).pipe(defaultIfEmpty('carbonara')); // $ExpectType Observable }); +it('should infer correctly with a subtype passed through parameters', () => { + const o = of(true, false).pipe(map(p => p), defaultIfEmpty(true)); // $ExpectType Observable +}); + it('should enforce types', () => { const o = of(1, 2, 3).pipe(defaultIfEmpty(4, 5)); // $ExpectError }); diff --git a/src/internal/operators/defaultIfEmpty.ts b/src/internal/operators/defaultIfEmpty.ts index 6d6123ea58..dc33ecf9be 100644 --- a/src/internal/operators/defaultIfEmpty.ts +++ b/src/internal/operators/defaultIfEmpty.ts @@ -4,8 +4,7 @@ import { Subscriber } from '../Subscriber'; import { OperatorFunction, MonoTypeOperatorFunction } from '../types'; /* tslint:disable:max-line-length */ -export function defaultIfEmpty(defaultValue?: T): MonoTypeOperatorFunction; -export function defaultIfEmpty(defaultValue?: R): OperatorFunction; +export function defaultIfEmpty(defaultValue?: R): OperatorFunction; /* tslint:enable:max-line-length */ /** diff --git a/src/internal/operators/first.ts b/src/internal/operators/first.ts index 7dfde4228a..ef55a79cb7 100644 --- a/src/internal/operators/first.ts +++ b/src/internal/operators/first.ts @@ -84,6 +84,6 @@ export function first( return (source: Observable) => source.pipe( predicate ? filter((v, i) => predicate(v, i, source)) : identity, take(1), - hasDefaultValue ? defaultIfEmpty(defaultValue) : throwIfEmpty(() => new EmptyError()), + hasDefaultValue ? defaultIfEmpty(defaultValue) : throwIfEmpty(() => new EmptyError()), ); } diff --git a/src/internal/operators/last.ts b/src/internal/operators/last.ts index 41b3e9332c..64ee0b1f65 100644 --- a/src/internal/operators/last.ts +++ b/src/internal/operators/last.ts @@ -47,6 +47,6 @@ export function last( return (source: Observable) => source.pipe( predicate ? filter((v, i) => predicate(v, i, source)) : identity, takeLast(1), - hasDefaultValue ? defaultIfEmpty(defaultValue) : throwIfEmpty(() => new EmptyError()), + hasDefaultValue ? defaultIfEmpty(defaultValue) : throwIfEmpty(() => new EmptyError()), ); }