andywer / threads.js

🧵 Make web workers & worker threads as simple as a function call.
https://threads.js.org/
MIT License
3.04k stars 161 forks source link

fix: add "types" field to conditional exports #470

Closed TomAFrench closed 2 months ago

TomAFrench commented 1 year ago

I'm experiencing a lot of issues when trying to resolve the types

src/fft/single_fft.ts:1:26 - error TS7016: Could not find a declaration file for module 'threads'. '/home/tom/Programming/turbo-prover-js/node_modules/threads/index.mjs' implicitly has an 'any' type.
  There are types at '/home/tom/Programming/turbo-prover-js/node_modules/threads/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'threads' library may need to update its package.json or typings.

1 import { Transfer } from 'threads';
                           ~~~~~~~~~

src/pippenger/single_pippenger.ts:3:26 - error TS7016: Could not find a declaration file for module 'threads'. '/home/tom/Programming/turbo-prover-js/node_modules/threads/index.mjs' implicitly has an 'any' type.
  There are types at '/home/tom/Programming/turbo-prover-js/node_modules/threads/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'threads' library may need to update its package.json or typings.

3 import { Transfer } from 'threads';
                           ~~~~~~~~~

src/wasm/worker.ts:2:37 - error TS7016: Could not find a declaration file for module 'threads/observable'. '/home/tom/Programming/turbo-prover-js/node_modules/threads/observable.mjs' implicitly has an 'any' type.
  Try `npm i --save-dev @types/threads` if it exists or add a new declaration (.d.ts) file containing `declare module 'threads/observable';`

2 import { Subject, Observable } from 'threads/observable';
                                      ~~~~~~~~~~~~~~~~~~~~

src/wasm/worker.ts:3:34 - error TS7016: Could not find a declaration file for module 'threads/worker'. '/home/tom/Programming/turbo-prover-js/node_modules/threads/worker.mjs' implicitly has an 'any' type.
  Try `npm i --save-dev @types/threads` if it exists or add a new declaration (.d.ts) file containing `declare module 'threads/worker';`

3 import { expose, Transfer } from 'threads/worker';
                                   ~~~~~~~~~~~~~~~~

src/wasm/worker_factory.ts:2:39 - error TS7016: Could not find a declaration file for module 'threads'. '/home/tom/Programming/turbo-prover-js/node_modules/threads/index.mjs' implicitly has an 'any' type.
  There are types at '/home/tom/Programming/turbo-prover-js/node_modules/threads/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'threads' library may need to update its package.json or typings.

2 import { spawn, Thread, Worker } from 'threads';

The last error explicitly calls out that changes need to be made to the threads.js package.json. I've then added a "types" field for each of the conditional exports.

TomAFrench commented 1 year ago

Related to #462

tien commented 1 year ago

default needs to come last else it will throw an error

CMCDragonkai commented 1 year ago

Yea the types in this package doesn't work. In my vscode it says:

Could not find a declaration file for module 'threads'. '/home/cmcdragonkai/Projects/js-workers/node_modules/threads/index.mjs' implicitly has an 'any' type.
  There are types at '/home/cmcdragonkai/Projects/js-workers/node_modules/threads/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'threads' library may need to update its package.json or typings.ts(7016)

This with native ESM support.

When I was using CJS, I could directly import the type files, hacking around the dist. But with ESM, I think you need to specify the types key properly above require, and default... etc.

CMCDragonkai commented 1 year ago

These are the types I care about:

import type { ModuleThread } from 'threads';
import type { ModuleMethods } from 'threads/dist/types/master';
import type { QueuedTask } from 'threads/dist/master/pool-types';

They used to work when I was using CJS. Now I'm transitioning to ESM, and the last 2 imports no longer work.

TomAFrench commented 1 year ago

@Altrue @tien Ah, thank you. I must admit I didn't test that as a required ordering to fields in JSON doesn't really make sense.

I can't quite remember why I was running into this issue anymore (most likely a side project I'm not particularly active in Ah, this was for work but we've gone down a different path) so this isn't a huge priority to me anymore and @andywer doesn't seem to be actively merging PRs currently.

CMCDragonkai commented 11 months ago

@andywer are you still working on this? Without this PR I think ESM downstream projects can't use this.

And I actually care about these types: https://github.com/andywer/threads.js/pull/470#issuecomment-1676248028

Anton-Plagemann commented 2 months ago

Hi @andywer, I've ran into the same issue, fixed them with the changes from this PR (using patch-package). Would be great if you could merge it 🙏

andywer commented 2 months ago

Hi @Anton-Plagemann & @TomAFrench, sorry for the unresponsiveness!

Will merge right away.

andywer commented 2 months ago

Tests are broken, unfortunately. Updated some dependencies on https://github.com/andywer/threads.js/tree/chore/fix-ci, but still have an issue with a worker in a browser. The statically deployed worker script doesn't exist anymore… 😕

oxcl commented 2 months ago

@andywer hi. is there anything that needs to be done to speed up the process so that we can get a new release with this fix in npm?

andywer commented 2 months ago

@oxcl If someone could fix the build, that would be amazing. I could also build and publish despite the failing test, but we should definitely fix this in the mid-term.

oxcl commented 2 months ago

@oxcl If someone could fix the build, that would be amazing. I could also build and publish despite the failing test, but we should definitely fix this in the mid-term.

@andywer I just ran npm audit fix on the project and it seems like the problem went away. both npm run build and npm run test run successfully. should i submit a PR?

andywer commented 2 months ago

@oxcl Very hard to believe. The worker script deployment doesn't exist anymore: https://github.com/andywer/threads.js/blob/5f9a15f25bf6b4e8c79c51a63f6757c5b28d0f50/test-tooling/webpack/app.ts#L41

Unfortunately, I also don't know if I have the code anywhere else, but I think it was just the transpiled and bundled hello-world.ts worker IIRC. Maybe we don't even need bundling anymore and can just use ES module imports from esm.sh or so.