denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
93.99k stars 5.23k forks source link

deno lsp, deno-ts: Property 'postMessage' does not exist on type 'Window & typeof globalThis'.deno-ts(2339) #9696

Open starkpsmichael opened 3 years ago

starkpsmichael commented 3 years ago

Using deno canary, latest stable vscode and vscode-deno ext.

Don't know who produce this problem but i'm trying to solve the problem that some type definitions aren't available in webworkers:

Create a file called a.ts

const worker = new Worker(new URL("./b.ts", import.meta.url).href, {
  type: "module",
});

worker.addEventListener("message", (b) => {
  globalThis.console.log(b);
});

Create a file called b.ts

self.postMessage("1");

deno run a.ts works - with no errors, but hovering over postMessage gives: Property 'postMessage' does not exist on type 'Window & typeof globalThis'.deno-ts(2339)

Trying to solve this with a tripple slash directive at the top of the file:

/// <reference lib="webworker" />
self.postMessage("1"); // Hovering works now
but `deno run` now gives the following errors: ```sh deno run --allow-read a.ts Check file:///workspaces/fk/b.ts error: Uncaught (in worker "") TS6200 [ERROR]: Definitions of the following identifiers conflict with those in another file: DOMException, Event, EventTarget, EventListenerOrEventListenerObject, ProgressEvent, TextDecoder, TextEncoder, AbortController, URLSearchParams, URL interface AddEventListenerOptions extends EventListenerOptions { ~~~~~~~~~ at asset:///lib.webworker.d.ts:25:1 Conflicts are in this file. declare class DOMException extends Error { ~~~~~~~ at asset:///lib.deno.web.d.ts:8:1 TS6200 [ERROR]: Definitions of the following identifiers conflict with those in another file: Global, Instance, Memory, Module, Table, ImportExportKind, TableKind, ValueType, ExportValue, Exports, ImportValue, ModuleImports, Imports, BufferSource, MessageEvent, ErrorEvent, Worker, PerformanceEntryList, Performance, PerformanceEntry, PerformanceMark, PerformanceMeasure, CustomEvent interface AddEventListenerOptions extends EventListenerOptions { ~~~~~~~~~ at asset:///lib.webworker.d.ts:25:1 Conflicts are in this file. declare namespace WebAssembly { ~~~~~~~ at asset:///lib.deno.shared_globals.d.ts:13:1 TS6200 [ERROR]: Definitions of the following identifiers conflict with those in another file: ReadableStreamReader, CountQueuingStrategy, ByteLengthQueuingStrategy, BlobPart, Blob, File, FormDataEntryValue, FormData, HeadersInit, Headers, RequestInfo, RequestCache, RequestCredentials, RequestMode, RequestRedirect, ReferrerPolicy, BodyInit, RequestDestination, Request, ResponseType, Response interface AddEventListenerOptions extends EventListenerOptions { ~~~~~~~~~ at asset:///lib.webworker.d.ts:25:1 Conflicts are in this file. interface DomIterable { ~~~~~~~~~ at asset:///lib.deno.fetch.d.ts:8:1 TS2300 [ERROR]: Duplicate identifier 'CloseEvent'. interface CloseEvent extends Event { ~~~~~~~~~~ at asset:///lib.webworker.d.ts:965:11 'CloseEvent' was also declared here. declare class CloseEvent extends Event { ~~~~~~~~~~ at asset:///lib.deno.websocket.d.ts:14:15 TS2300 [ERROR]: Duplicate identifier 'CloseEvent'. declare var CloseEvent: { ~~~~~~~~~~ at asset:///lib.webworker.d.ts:980:13 'CloseEvent' was also declared here. declare class CloseEvent extends Event { ~~~~~~~~~~ at asset:///lib.deno.websocket.d.ts:14:15 TS2300 [ERROR]: Duplicate identifier 'DedicatedWorkerGlobalScope'. interface DedicatedWorkerGlobalScope extends WorkerGlobalScope, AnimationFrameProvider { ~~~~~~~~~~~~~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:1304:11 'DedicatedWorkerGlobalScope' was also declared here. declare class DedicatedWorkerGlobalScope extends WorkerGlobalScope { ~~~~~~~~~~~~~~~~~~~~~~~~~~ at asset:///lib.deno.worker.d.ts:42:15 TS2300 [ERROR]: Duplicate identifier 'DedicatedWorkerGlobalScope'. declare var DedicatedWorkerGlobalScope: { ~~~~~~~~~~~~~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:1326:13 'DedicatedWorkerGlobalScope' was also declared here. declare class DedicatedWorkerGlobalScope extends WorkerGlobalScope { ~~~~~~~~~~~~~~~~~~~~~~~~~~ at asset:///lib.deno.worker.d.ts:42:15 TS2430 [ERROR]: Interface 'ReadableStreamDefaultReader' incorrectly extends interface 'ReadableStreamGenericReader'. Types of property 'closed' are incompatible. Type 'Promise' is not assignable to type 'Promise'. Type 'void' is not assignable to type 'undefined'. interface ReadableStreamDefaultReader extends ReadableStreamGenericReader { ~~~~~~~~~~~~~~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:2772:11 TS2300 [ERROR]: Duplicate identifier 'WebSocket'. interface WebSocket extends EventTarget { ~~~~~~~~~ at asset:///lib.webworker.d.ts:5324:11 'WebSocket' was also declared here. declare class WebSocket extends EventTarget { ~~~~~~~~~ at asset:///lib.deno.websocket.d.ts:38:15 TS2300 [ERROR]: Duplicate identifier 'WebSocket'. declare var WebSocket: { ~~~~~~~~~ at asset:///lib.webworker.d.ts:5375:13 'WebSocket' was also declared here. declare class WebSocket extends EventTarget { ~~~~~~~~~ at asset:///lib.deno.websocket.d.ts:38:15 TS2300 [ERROR]: Duplicate identifier 'WorkerGlobalScope'. interface WorkerGlobalScope extends EventTarget, WindowOrWorkerGlobalScope { ~~~~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:5456:11 'WorkerGlobalScope' was also declared here. declare class WorkerGlobalScope { ~~~~~~~~~~~~~~~~~ at asset:///lib.deno.worker.d.ts:9:15 TS2300 [ERROR]: Duplicate identifier 'WorkerGlobalScope'. declare var WorkerGlobalScope: { ~~~~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:5482:13 'WorkerGlobalScope' was also declared here. declare class WorkerGlobalScope { ~~~~~~~~~~~~~~~~~ at asset:///lib.deno.worker.d.ts:9:15 TS2300 [ERROR]: Duplicate identifier 'WorkerLocation'. interface WorkerLocation { ~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:5488:11 'WorkerLocation' was also declared here. declare class WorkerLocation { ~~~~~~~~~~~~~~ at asset:///lib.deno.worker.d.ts:75:15 TS2300 [ERROR]: Duplicate identifier 'WorkerLocation'. declare var WorkerLocation: { ~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:5501:13 'WorkerLocation' was also declared here. declare class WorkerLocation { ~~~~~~~~~~~~~~ at asset:///lib.deno.worker.d.ts:75:15 TS2300 [ERROR]: Duplicate identifier 'close'. declare function close(): void; ~~~~~ at asset:///lib.webworker.d.ts:5905:18 'close' was also declared here. declare var close: () => void; ~~~~~ at asset:///lib.deno.worker.d.ts:66:13 TS2300 [ERROR]: Duplicate identifier 'postMessage'. declare function postMessage(message: any, transfer: Transferable[]): void; ~~~~~~~~~~~ at asset:///lib.webworker.d.ts:5909:18 'postMessage' was also declared here. declare var postMessage: (message: any) => void; ~~~~~~~~~~~ at asset:///lib.deno.worker.d.ts:68:13 TS2300 [ERROR]: Duplicate identifier 'postMessage'. declare function postMessage(message: any, options?: PostMessageOptions): void; ~~~~~~~~~~~ at asset:///lib.webworker.d.ts:5910:18 'postMessage' was also declared here. declare var postMessage: (message: any) => void; ~~~~~~~~~~~ at asset:///lib.deno.worker.d.ts:68:13 TS2300 [ERROR]: Duplicate identifier 'BinaryType'. type BinaryType = "arraybuffer" | "blob"; ~~~~~~~~~~ at asset:///lib.webworker.d.ts:6002:6 'BinaryType' was also declared here. type BinaryType = "arraybuffer" | "blob"; ~~~~~~~~~~ at asset:///lib.deno.websocket.d.ts:112:6 TS2300 [ERROR]: Duplicate identifier 'WorkerGlobalScope'. declare class WorkerGlobalScope { ~~~~~~~~~~~~~~~~~ at asset:///lib.deno.worker.d.ts:9:15 'WorkerGlobalScope' was also declared here. interface WorkerGlobalScope extends EventTarget, WindowOrWorkerGlobalScope { ~~~~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:5456:11 and here. declare var WorkerGlobalScope: { ~~~~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:5482:13 TS2300 [ERROR]: Duplicate identifier 'DedicatedWorkerGlobalScope'. declare class DedicatedWorkerGlobalScope extends WorkerGlobalScope { ~~~~~~~~~~~~~~~~~~~~~~~~~~ at asset:///lib.deno.worker.d.ts:42:15 'DedicatedWorkerGlobalScope' was also declared here. interface DedicatedWorkerGlobalScope extends WorkerGlobalScope, AnimationFrameProvider { ~~~~~~~~~~~~~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:1304:11 and here. declare var DedicatedWorkerGlobalScope: { ~~~~~~~~~~~~~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:1326:13 TS2403 [ERROR]: Subsequent variable declarations must have the same type. Variable 'onmessage' must be of type '((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null', but here has type '((this: WorkerGlobalScope & typeof globalThis, ev: MessageEvent) => any) | null'. declare var onmessage: ~~~~~~~~~ at asset:///lib.deno.worker.d.ts:48:13 'onmessage' was also declared here. declare var onmessage: ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null; ~~~~~~~~~ at asset:///lib.webworker.d.ts:5900:13 TS2403 [ERROR]: Subsequent variable declarations must have the same type. Variable 'onmessageerror' must be of type '((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null', but here has type '((this: WorkerGlobalScope & typeof globalThis, ev: MessageEvent) => any) | null'. declare var onmessageerror: ~~~~~~~~~~~~~~ at asset:///lib.deno.worker.d.ts:54:13 'onmessageerror' was also declared here. declare var onmessageerror: ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null; ~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:5901:13 TS2403 [ERROR]: Subsequent variable declarations must have the same type. Variable 'onerror' must be of type '((this: DedicatedWorkerGlobalScope, ev: ErrorEvent) => any) | null', but here has type '((this: WorkerGlobalScope & typeof globalThis, ev: ErrorEvent) => any) | null'. declare var onerror: ~~~~~~~ at asset:///lib.deno.worker.d.ts:60:13 'onerror' was also declared here. declare var onerror: ((this: DedicatedWorkerGlobalScope, ev: ErrorEvent) => any) | null; ~~~~~~~ at asset:///lib.webworker.d.ts:5920:13 TS2300 [ERROR]: Duplicate identifier 'close'. declare var close: () => void; ~~~~~ at asset:///lib.deno.worker.d.ts:66:13 'close' was also declared here. declare function close(): void; ~~~~~ at asset:///lib.webworker.d.ts:5905:18 TS2300 [ERROR]: Duplicate identifier 'postMessage'. declare var postMessage: (message: any) => void; ~~~~~~~~~~~ at asset:///lib.deno.worker.d.ts:68:13 'postMessage' was also declared here. declare function postMessage(message: any, transfer: Transferable[]): void; ~~~~~~~~~~~ at asset:///lib.webworker.d.ts:5909:18 and here. declare function postMessage(message: any, options?: PostMessageOptions): void; ~~~~~~~~~~~ at asset:///lib.webworker.d.ts:5910:18 TS2300 [ERROR]: Duplicate identifier 'WorkerLocation'. declare class WorkerLocation { ~~~~~~~~~~~~~~ at asset:///lib.deno.worker.d.ts:75:15 'WorkerLocation' was also declared here. interface WorkerLocation { ~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:5488:11 and here. declare var WorkerLocation: { ~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:5501:13 TS6200 [ERROR]: Definitions of the following identifiers conflict with those in another file: Global, Instance, Memory, Module, Table, ImportExportKind, TableKind, ValueType, ExportValue, Exports, ImportValue, ModuleImports, Imports, BufferSource, MessageEvent, ErrorEvent, Worker, PerformanceEntryList, Performance, PerformanceEntry, PerformanceMark, PerformanceMeasure, CustomEvent declare namespace WebAssembly { ~~~~~~~ at asset:///lib.deno.shared_globals.d.ts:13:1 Conflicts are in this file. interface AddEventListenerOptions extends EventListenerOptions { ~~~~~~~~~ at asset:///lib.webworker.d.ts:25:1 TS2375 [ERROR]: Duplicate number index signature. [index: number]: string; ~~~~~~~~~~~~~~~~~~~~~~~~ at asset:///lib.deno.shared_globals.d.ts:388:3 TS6200 [ERROR]: Definitions of the following identifiers conflict with those in another file: ReadableStreamReader, CountQueuingStrategy, ByteLengthQueuingStrategy, BlobPart, Blob, File, FormDataEntryValue, FormData, HeadersInit, Headers, RequestInfo, RequestCache, RequestCredentials, RequestMode, RequestRedirect, ReferrerPolicy, BodyInit, RequestDestination, Request, ResponseType, Response interface DomIterable { ~~~~~~~~~ at asset:///lib.deno.fetch.d.ts:8:1 Conflicts are in this file. interface AddEventListenerOptions extends EventListenerOptions { ~~~~~~~~~ at asset:///lib.webworker.d.ts:25:1 TS2403 [ERROR]: Subsequent variable declarations must have the same type. Variable 'ReadableStreamDefaultReader' must be of type '{ new (stream: ReadableStream): ReadableStreamDefaultReader; prototype: ReadableStreamDefaultReader; }', but here has type '{ new (stream: ReadableStream): ReadableStreamDefaultReader; prototype: ReadableStreamDefaultReader; }'. declare var ReadableStreamDefaultReader: { ~~~~~~~~~~~~~~~~~~~~~~~~~~~ at asset:///lib.deno.fetch.d.ts:40:13 'ReadableStreamDefaultReader' was also declared here. declare var ReadableStreamDefaultReader: { ~~~~~~~~~~~~~~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:2777:13 TS2314 [ERROR]: Generic type 'ReadableStreamReader' requires 1 type argument(s). prototype: ReadableStreamReader; ~~~~~~~~~~~~~~~~~~~~ at asset:///lib.deno.fetch.d.ts:52:14 TS2314 [ERROR]: Generic type 'ReadableStreamReader' requires 1 type argument(s). new (): ReadableStreamReader; ~~~~~~~~~~~~~~~~~~~~ at asset:///lib.deno.fetch.d.ts:53:11 TS2403 [ERROR]: Subsequent variable declarations must have the same type. Variable 'ReadableStream' must be of type '{ new (underlyingSource?: UnderlyingSource | undefined, strategy?: QueuingStrategy | undefined): ReadableStream; prototype: ReadableStream<...>; }', but here has type '{ new (underlyingSource: UnderlyingByteSource, strategy?: { highWaterMark?: number | undefined; size?: undefined; } | undefined): ReadableStream; new (underlyingSource?: UnderlyingSource<...> | undefined, strategy?: QueuingStrategy<...> | undefined): ReadableStream<...>; prototype: ReadableStrea...'. declare var ReadableStream: { ~~~~~~~~~~~~~~ at asset:///lib.deno.fetch.d.ts:174:13 'ReadableStream' was also declared here. declare var ReadableStream: { ~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:2755:13 TS2717 [ERROR]: Subsequent property declarations must have the same type. Property 'closed' must be of type 'Promise', but here has type 'Promise'. readonly closed: Promise; ~~~~~~ at asset:///lib.deno.fetch.d.ts:236:12 'closed' was also declared here. readonly closed: Promise; ~~~~~~ at asset:///lib.webworker.d.ts:5541:14 TS2717 [ERROR]: Subsequent property declarations must have the same type. Property 'ready' must be of type 'Promise', but here has type 'Promise'. readonly ready: Promise; ~~~~~ at asset:///lib.deno.fetch.d.ts:238:12 'ready' was also declared here. readonly ready: Promise; ~~~~~ at asset:///lib.webworker.d.ts:5543:14 TS2403 [ERROR]: Subsequent variable declarations must have the same type. Variable 'WritableStreamDefaultWriter' must be of type '{ new (stream: WritableStream): WritableStreamDefaultWriter; prototype: WritableStreamDefaultWriter; }', but here has type '{ new (): WritableStreamDefaultWriter; prototype: WritableStreamDefaultWriter; }'. declare var WritableStreamDefaultWriter: { ~~~~~~~~~~~~~~~~~~~~~~~~~~~ at asset:///lib.deno.fetch.d.ts:245:13 'WritableStreamDefaultWriter' was also declared here. declare var WritableStreamDefaultWriter: { ~~~~~~~~~~~~~~~~~~~~~~~~~~~ at asset:///lib.webworker.d.ts:5550:13 TS2300 [ERROR]: Duplicate identifier 'CloseEvent'. declare class CloseEvent extends Event { ~~~~~~~~~~ at asset:///lib.deno.websocket.d.ts:14:15 'CloseEvent' was also declared here. interface CloseEvent extends Event { ~~~~~~~~~~ at asset:///lib.webworker.d.ts:965:11 and here. declare var CloseEvent: { ~~~~~~~~~~ at asset:///lib.webworker.d.ts:980:13 TS2300 [ERROR]: Duplicate identifier 'WebSocket'. declare class WebSocket extends EventTarget { ~~~~~~~~~ at asset:///lib.deno.websocket.d.ts:38:15 'WebSocket' was also declared here. interface WebSocket extends EventTarget { ~~~~~~~~~ at asset:///lib.webworker.d.ts:5324:11 and here. declare var WebSocket: { ~~~~~~~~~ at asset:///lib.webworker.d.ts:5375:13 TS2300 [ERROR]: Duplicate identifier 'BinaryType'. type BinaryType = "arraybuffer" | "blob"; ~~~~~~~~~~ at asset:///lib.deno.websocket.d.ts:112:6 'BinaryType' was also declared here. type BinaryType = "arraybuffer" | "blob"; ~~~~~~~~~~ at asset:///lib.webworker.d.ts:6002:6 TS2717 [ERROR]: Subsequent property declarations must have the same type. Property 'subtle' must be of type 'SubtleCrypto', but here has type 'null'. readonly subtle: null; ~~~~~~ at asset:///lib.deno.crypto.d.ts:9:12 'subtle' was also declared here. readonly subtle: SubtleCrypto; ~~~~~~ at asset:///lib.webworker.d.ts:1007:14 TS6200 [ERROR]: Definitions of the following identifiers conflict with those in another file: DOMException, Event, EventTarget, EventListenerOrEventListenerObject, ProgressEvent, TextDecoder, TextEncoder, AbortController, URLSearchParams, URL declare class DOMException extends Error { ~~~~~~~ at asset:///lib.deno.web.d.ts:8:1 Conflicts are in this file. interface AddEventListenerOptions extends EventListenerOptions { ~~~~~~~~~ at asset:///lib.webworker.d.ts:25:1 Found 41 errors. error: Uncaught (in promise) Error: Unhandled error event reached main worker. at Worker.#poll (deno:runtime/js/11_workers.js:269:21) ```
kitsonk commented 3 years ago

Related to #8219 and others.

We need workspace folders and a config flag that indicates that files are worker scripts.

Mesteery commented 3 years ago

What about DOMException, Event, EventTarget, EventListenerOrEventListenerObject, ProgressEvent, TextDecoder, TextEncoder, TextDecoderStream, TextEncoderStream, AbortController, BlobPart, Blob, File, ReadableStreamReader, CountQueuingStrategy, ByteLengthQueuingStrategy, .... which do not depend on workers?

kitsonk commented 3 years ago

@Mesteery those currently work. If you have issues with them being available it is not related to this feature request. I would open a new issue in vscode_deno with reproduction information.

Mesteery commented 3 years ago

Yes they work but not when accessed from globalThis: globalThis.ByteLengthQueuingStrategy, for example, raises a error :

error: TS2339 [ERROR]: Property 'ByteLengthQueuingStrategy' does not exist on type 'typeof globalThis'.
globalThis.ByteLengthQueuingStrategy
eight04 commented 11 months ago

Is there a workaround to this?

NewYanko commented 11 months ago

Is there a workaround to this?

Also wondering. Is there a way to flag a file as being a worker?

mahdiyari commented 3 months ago

Similar

Property 'onmessage' does not exist on type 'Window & typeof globalThis'.deno-ts(2339)
mommysgoodpuppy commented 2 months ago

Is there a workaround to this?

My workaround is to do this. const worker = self as unknown as Worker;

mahdiyari commented 2 months ago

You could ignore the line

// @ts-ignore: lack of types in deno
self.onmessage = (e: MessageEvent) => {}