-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Conversation
@@ -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 | |||
}); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>> { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
fixes #5446