Workers: Property 'onmessage' dos not exist
Im learning how to create worker threads but
deno-ts says: Property 'onmessage' does not exist on type 'Window & typeof globalThis'.
Code
Am I missing some configuration?7 Replies
Add these lines to the top of
worker.ts.
And if you run deno check as part of your workflow, instead of deno check main.ts worker.ts, do deno check main.ts and deno check worker.ts separately, or there may be false negatives.Thank you! Also, in what part of the documentation I can search to learn more about those top level comments?
These are triple-slash directives.
no-default-lib="true" means “don't include default libraries” (deno.window and deno.unstable in Deno's case), and lib="…" references the specified library.
I just read that no-default-lib might get deprecated soon. If that happens, the alternative would be to set compilerOptions.lib to [] in deno.json, and add a reference to deno.window to all non-worker scripts.I know of a package that makes using workers easier, but it is a bit of self advertisement. 😛
im curious
its also possible to do
export const self = globalThis as unknown as Worker
and then import self in workers but maybe a little cursedIt's going to receive better typings soon™️