Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(defer) allow passing () => any to observableFactory #5513

Merged
merged 2 commits into from Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions spec-dtslint/observables/defer-spec.ts
Expand Up @@ -16,6 +16,10 @@ it('should support union type returns', () => {
const a = defer(() => Math.random() > 0.5 ? of(123) : of('abc')); // $ExpectType Observable<string | number>
});

it('should infer correctly with function return any', () => {
const a = defer(() => 3 as any); // $ExpectType Observable<unknown>
});

it('should error with void functions', () => {
const a = defer(() => {}); // $ExpectError
});
Expand All @@ -28,8 +32,8 @@ it('should error if function returns undefined', () => {
const a = defer(() => undefined); // $ExpectError
});

it('should error if function returns never', () => {
const a = defer(() => { throw new Error(); }); // $ExpectError
it('should infer if function returns never', () => {
const a = defer(() => { throw new Error(); }); // $ExpectType Observable<never>
});


Expand All @@ -43,6 +47,6 @@ it('should infer correctly with function that sometimes error', () => {
});
});

it('should infer correctly with functions that sometimes do not return an ObservableInput', () => {
it('should error with functions that sometimes do not return an ObservableInput', () => {
const a = defer(() => { if (Math.random() < 0.5) { return of(42); } }); // $ExpectError
});
4 changes: 1 addition & 3 deletions src/internal/observable/defer.ts
Expand Up @@ -51,9 +51,7 @@ import { from } from './from'; // lol
* @name defer
* @owner Observable
*/
export function defer<R extends ObservableInput<any>>(
observableFactory: [R] extends [undefined] | [void] ? never : () => R
): Observable<ObservedValueOf<R>> {
export function defer<R extends ObservableInput<any>>(observableFactory: () => R): Observable<ObservedValueOf<R>> {
return new Observable<ObservedValueOf<R>>(subscriber => {
let input: R;
try {
Expand Down