jcayzac
jcayzac17mo ago

Worker: TS2304 [ERROR]: Cannot find name 'postMessage'

When type-checking a worker script that uses the global postMessage() method, deno check gives a TS2304 [ERROR]: Cannot find name 'postMessage' error. Is there any workaround?
6 Replies
jcayzac
jcayzacOP17mo ago
deno check doesn't seem to accept any argument to specify what context the script executes in (e.g. Window vs DedicatedWorkerGlobalScope), nor can it apparently infer it based on file name conventions (*.worker.ts)
AapoAlas
AapoAlas17mo ago
You'll need to use triple-slash type comment.
jcayzac
jcayzacOP17mo ago
Do you mean /// @deno-types="../worker.d.ts" and declare postMessage() in that file? Doesn't the /// comment only works on imports, tho? I'm not importing anything. Also, it just occured to me that a global postMessage() isn't specific to workers, and exists on the Window interface too. It's weird that Deno doesn't have it.
AapoAlas
AapoAlas17mo ago
No, I meant including (I think) lib.webworker.d.ts which should maybe have the definitions globally available. Maybe.
jcayzac
jcayzacOP17mo ago
Thanks, it's actually mentioned in the docs and I missed it. https://deno.land/manual@v1.35.2/advanced/typescript/types#using-triple-slash-directives
Deno
Types and Type Declarations | Manual | Deno
One of the design principles of Deno is no non-standard module resolution. When TypeScript is type checking a file, it only cares about the types for the file, and the tsc compiler has a lot of log
jcayzac
jcayzacOP17mo ago
/// <reference no-default-lib="true" />
/// <reference lib="deno.worker" />
/// <reference no-default-lib="true" />
/// <reference lib="deno.worker" />
It would be neat if lib could be assigned per file pattern, inthe json configuration (so that I could have the default lib everywhere but use deno.worker for *.worker.ts)