From 6101879e2705dfa065ff632b42c1fe9ea47ff674 Mon Sep 17 00:00:00 2001 From: Nicholas Jamieson Date: Wed, 5 Jun 2019 20:09:18 +1000 Subject: [PATCH 1/3] test(defer): add failing dtslint test --- spec-dtslint/observables/defer-spec.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec-dtslint/observables/defer-spec.ts b/spec-dtslint/observables/defer-spec.ts index cc0c2f1092..a0046d02e4 100644 --- a/spec-dtslint/observables/defer-spec.ts +++ b/spec-dtslint/observables/defer-spec.ts @@ -18,4 +18,8 @@ it('should support union type returns', () => { it('should infer correctly with void functions', () => { const a = defer(() => {}); // $ExpectType Observable +}); + +it('should error if an ObservableInput is not returned', () => { + const a = defer(() => 42); // $ExpectError }); \ No newline at end of file From 024c772485cfcc45953ff125bc2992c192b75e6f Mon Sep 17 00:00:00 2001 From: Nicholas Jamieson Date: Wed, 5 Jun 2019 20:14:29 +1000 Subject: [PATCH 2/3] fix(defer): restrict allowed factory types --- src/internal/observable/defer.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/internal/observable/defer.ts b/src/internal/observable/defer.ts index 29c1716a67..e396539e4f 100644 --- a/src/internal/observable/defer.ts +++ b/src/internal/observable/defer.ts @@ -52,18 +52,16 @@ import { empty } from './empty'; * @name defer * @owner Observable */ -export function defer>(observableFactory: () => O): Observable>; -export function defer(observableFactory: () => void): Observable; -export function defer>(observableFactory: () => O | void): Observable> { - return new Observable>(subscriber => { - let input: O | void; +export function defer | void>(observableFactory: () => R): Observable> { + return new Observable>(subscriber => { + let input: R | void; try { input = observableFactory(); } catch (err) { subscriber.error(err); return undefined; } - const source = input ? from(input) : empty(); + const source = input ? from(input as ObservableInput>) : empty(); return source.subscribe(subscriber); }); } From b0b2cc6f55ef95ccf0d482c4ac19fecd097ff1df Mon Sep 17 00:00:00 2001 From: Nicholas Jamieson Date: Thu, 6 Jun 2019 03:36:12 +1000 Subject: [PATCH 3/3] test(defer): add a sometimes-returns dtslint test --- spec-dtslint/observables/defer-spec.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec-dtslint/observables/defer-spec.ts b/spec-dtslint/observables/defer-spec.ts index a0046d02e4..42aa3a672b 100644 --- a/spec-dtslint/observables/defer-spec.ts +++ b/spec-dtslint/observables/defer-spec.ts @@ -22,4 +22,8 @@ it('should infer correctly with void functions', () => { it('should error if an ObservableInput is not returned', () => { const a = defer(() => 42); // $ExpectError -}); \ No newline at end of file +}); + +it('should infer correctly with functions that sometimes do not return an ObservableInput', () => { + const a = defer(() => { if (Math.random() < 0.5) { return of(42); } }); // $ExpectType Observable +});