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) Disallow passing () => undefined to observableFactory #5449

Merged
merged 6 commits into from
Jun 15, 2020

Conversation

kolodny
Copy link
Member

@kolodny kolodny commented May 20, 2020

fixes #5446

@kolodny kolodny requested review from benlesh and cartant May 20, 2020 14:55
Moshe Kolodny added 2 commits May 20, 2020 12:20
@@ -28,6 +28,10 @@ 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
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work?

defer(() => {
   if (Math.random() > 0.5) {
      throw new Error();
   }
   return of(1, 2, 3);
});

I mean, I assume it does, but It would be nice to have a test there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

export function defer<R extends ObservableInput<any> | void>(observableFactory: () => R): Observable<ObservedValueOf<R>> {
export function defer<R extends ObservableInput<any> | void>(
observableFactory: [R] extends [undefined] | [void] ? never : () => R
): Observable<ObservedValueOf<R>> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, if we are going to fix this footgun, we should only be accepting factories that return a valid ObservableInput. I think the fix should be the removal of the void:

export function defer<R extends ObservableInput<any>>(observableFactory: () => R): Observable<ObservedValueOf<R>> { ...

And I think the implementation should be changed, too. If it's not, the footgun is still there for non-TS usage.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It's weird that we ever had this work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I've updated the tests and dstlint spec to reflect the changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

defer(() => undefined) should be a TS error
3 participants