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 TypeScript types #451

Merged
merged 8 commits into from
Apr 27, 2020
Merged

Fix TypeScript types #451

merged 8 commits into from
Apr 27, 2020

Conversation

felixfbecker
Copy link
Contributor

@felixfbecker felixfbecker commented Apr 22, 2020

This fixes the typings issues described in #432.

  • Promisify<T> needs to check first whether T extends Promise already. Otherwise it could resolve to Promise<Promise<T>> if T already is a Promise (which is impossible in comlink and JavaScript in general).

  • In the mapped types, each right side generally needs to be extracted into its own type alias. This is because generic type aliases will distribute over union types, while inlining the type would not. This means the type would not work properly when union types are involved. This may sound like an edge case, but includes e.g. optional properties, which are union types xxx | undefined.

  • (most complicated part) For proxied functions, while the return type needs to be wrapped in Remote<T>, the parameters of the functions need to have the inverse applied. This is because from the point of view of the function implementation, passed callbacks will now return Promises, and therefor they need to type their signature accordingly. But the caller does not actually need to provide a callback that returns a Promise, from the point of view of the caller it just needs to pass a synchronous callback.

    • This means the library needs to define symmetric, recursive types for "remotification" and "deremotification". Within either, parameters get treated with the inverse transformation respectively, recursively.
    • The new methods [releaseProxy] and [createEndpoint] also need to be considered (i.e. not required to be provided by a caller).

The first commit adds tests that fail on master. I upgraded to TypeScript 3.9 to get access to @ts-expect-error and added a tsconfig.json so that strict mode is applied to the tests too.

The second commit overhauls the actual types to fix all the failing tests.

I did my best to write as many comments as possible to explain them, but the types are no doubt complex. The result is that users don't have to worry about any of this because it "just works" under the hood :)

Also fixes #424 in a type-safe way.

@felixfbecker felixfbecker force-pushed the fix-types branch 2 times, most recently from cc2f421 to 0082ce7 Compare April 22, 2020 21:33
@felixfbecker felixfbecker marked this pull request as ready for review April 22, 2020 21:39
@surma
Copy link
Collaborator

surma commented Apr 23, 2020

This is stellar work, @felixfbecker. Thanks for that. I gave it a skim, but gonna need a bit more time to look at it in detail.

Either way, thanks for working through this. I’ll get back to this soon!

Copy link
Collaborator

@surma surma left a comment

Choose a reason for hiding this comment

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

This is amazing work! Than you very much!

The only major thing that needs fixing (I think) is the fact that proxyMarked is being used on the remote side.

Other than that I’m happy to merge it once TS 3.9 goes stable.

@felixfbecker
Copy link
Contributor Author

Thanks for the review! I addressed all comments :)

@surma surma merged commit bf179bf into GoogleChromeLabs:master Apr 27, 2020
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.

2 participants