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: typedef default onfulfilled handler for E.when #1233

Merged
merged 1 commit into from
Jul 7, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/eventual-send/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ interface EProxy {
* E.when(x, res, rej) is equivalent to
* HandledPromise.resolve(x).then(res, rej)
*/
readonly when: <T, U>(
readonly when: <T, U = Awaited<T>>(
x: T,
onfulfilled?: (value: Awaited<T>) => ERef<U>,
onrejected?: (reason: any) => ERef<U>,
Expand Down
23 changes: 23 additions & 0 deletions packages/eventual-send/src/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,26 @@ const foo2 = async (a: FarRef<{ bar(): string; baz: number }>) => {
// @ts-expect-error - calling directly is valid but not yet in the typedef
a.bar;
};

// when
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're adding when type tests, do you mind also adding a test for the "normal" use case with a onfulfilled callback?

const aPromise = Promise.resolve('a');
const onePromise = Promise.resolve(1);
const remoteString: ERef<string> = Promise.resolve('remote');
E.when(Promise.all([aPromise, onePromise, remoteString])).then(
([str, num, remote]) => {
Comment on lines +54 to +55
Copy link
Contributor

Choose a reason for hiding this comment

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

The problem here is likely in a misusage of E.when, which is expected to be used like the following:

Suggested change
E.when(Promise.all([aPromise, onePromise, remoteString])).then(
([str, num, remote]) => {
E.when(Promise.all([aPromise, onePromise, remoteString]), ([str, num, remote]) => {

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@mhofman mhofman Jul 7, 2022

Choose a reason for hiding this comment

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

Right, but I think it's optional in the sense that the expectation is to use either onfulfilled or onrejected. Using .then() with E.when() is somewhat of an anti-pattern.

That said, to enable this case with the types, I do believe that defaulting U to T will 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.

"expectation" and "anti-pattern" are fuzzy to me wrt the type defs. The type checker has only errors not warnings :)

If you think the type should require at least one handler, I can define the type that way. Though I think you're saying that it should allow them both to be omitted and your solution sounds right. I'll try it.

Copy link
Member

Choose a reason for hiding this comment

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

Speaking only for then, the callback and errback default to exactly value => value and error => { throw error } respectively. They can both be omitted. when should be similar.

expectType<string>(str);
expectType<number>(num);
expectType<string>(remote);
},
);
E.when(
Promise.all([aPromise, onePromise, remoteString]),
([str, num, remote]) => {
expectType<string>(str);
expectType<number>(num);
expectType<string>(remote);
return { something: 'new' };
},
).then(result => {
expectType<{ something: string }>(result);
});