GoogleChromeLabs / comlink

Comlink makes WebWorkers enjoyable.
Apache License 2.0
11k stars 382 forks source link

Better TypeScript DX and an TypeScript Example #664

Open HuakunShen opened 2 weeks ago

HuakunShen commented 2 weeks ago

Thank you for this useful project!

I came across this project and find it useful, but there is no TypeScript example in this repo. I wanted to know how well it works with TypeScript.

I thought it would be beneficial to include a TypeScript example with the package to demonstrate how it works and to enhance the experience with IntelliSense autocompletion.

I included a small TypeScript example I make when trying this package. Also enabled declarationMap feature.

Enable declarationMap in tsconfig.json for better TypeScript DX.

Now developers can go to real source code definition of functions in .ts files rather than .d.ts files.

Replaced @rollup/plugin-typescript with rollup-plugin-typescript2

@rollup/plugin-typescript had some problem with declarationMap when generated .js and .d.ts files are not under dist directly, but under dist/esm or dist/umd.

@rollup/plugin-typescript generates "sources": ["../src/comlink.ts"] in comlink.d.ts.map, which is wrong.

rollup-plugin-typescript2 generates "sources": ["../../src/comlink.ts"],

{
  "version": 3,
  "file": "comlink.d.ts",
  "sourceRoot": "",
  "sources": ["../../src/comlink.ts"],
  "sources": ["../src/comlink.ts"], # this is wrong
  "names": []
}

Add a TypeScript Example

There is no TypeScript example in the docs/examples/99-nonworker-examples/iframe-ts directory. I added one so people can experiment with TypeScript features and type intellisense.

google-cla[bot] commented 2 weeks ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.