threads.js icon indicating copy to clipboard operation
threads.js copied to clipboard

fix: add "types" field to conditional exports

Open TomAFrench opened this issue 2 years ago • 6 comments

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 avatar Mar 24 '23 14:03 TomAFrench

Related to #462

TomAFrench avatar Mar 24 '23 20:03 TomAFrench

default needs to come last else it will throw an error

tien avatar May 21 '23 10:05 tien

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 avatar Aug 13 '23 06:08 CMCDragonkai

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.

CMCDragonkai avatar Aug 13 '23 06:08 CMCDragonkai

@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.

TomAFrench avatar Aug 13 '23 20:08 TomAFrench

@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

CMCDragonkai avatar Sep 26 '23 13:09 CMCDragonkai

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 🙏

Anton-Plagemann avatar Jun 19 '24 14:06 Anton-Plagemann

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

Will merge right away.

andywer avatar Jun 19 '24 14:06 andywer

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… 😕

andywer avatar Jun 19 '24 16:06 andywer

@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?

whoiscircuit avatar Jun 23 '24 13:06 whoiscircuit

@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 avatar Jun 23 '24 15:06 andywer

@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?

whoiscircuit avatar Jun 24 '24 07:06 whoiscircuit

@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.

andywer avatar Jun 24 '24 10:06 andywer