gpuweb / types

TypeScript type definitions for WebGPU https://gpuweb.github.io/types/
https://www.npmjs.com/package/@webgpu/types
BSD 3-Clause "New" or "Revised" License
181 stars 38 forks source link

Errors when using types with a web worker. #151

Open jozefchutka opened 3 months ago

jozefchutka commented 3 months ago

Following similar issue to https://github.com/gpuweb/types/issues/114 , I am getting:

node_modules/@webgpu/types/dist/index.d.ts:83:7 - error TS2552: Cannot find name 'HTMLImageElement'. Did you mean 'HTMLVideoElement'?

83     | HTMLImageElement
         ~~~~~~~~~~~~~~~~

Following change ( similar to https://github.com/gpuweb/types/pull/115/files ) would fix the build error

interface HTMLImageElement {}
kainino0x commented 3 months ago

huh, on that issue @toji was wondering why only HTMLVideoElement was a problem and nothing else. Wonder what the difference in config is.

The workaround would be fine for now but it's not the best, I'd like to be able to conditionally define some things only if DOM types are available. Like:

type IfDOM<T> = ('document' in typeof globalThis) ? T : never;

type GPUImageCopyExternalImageSource =

    | ImageBitmap
    | ImageData
    | IfDOM<HTMLImageElement>
    | IfDOM<HTMLVideoElement>
    | VideoFrame
    | IfDOM<HTMLCanvasElement>
    | OffscreenCanvas;

but that is not actually valid syntax. I'm sure something like that is possible though.

jozefchutka commented 3 months ago

Is there any way to provide multiple .d.ts files instead of just single index? Lets say worker.d.ts will provide types for a worker etc. I am not sure how that would be configured in tsconfig though.

kainino0x commented 3 months ago

I don't know. I tried looking it up yesterday, but couldn't find anything. But I think the idea I had would work if I could think of the syntax.

I believe that TypeScript's builtin types for dom/webworker just replicate the definitions for each environment.

kainino0x commented 3 months ago
type GetTypeIfConstructorDeclared<Name extends string> =
  typeof globalThis extends { [k in Name]: { prototype: infer T } } ? T : never;

type x = GetTypeIfConstructorDeclared<'HTMLImageElement'>;      // x == HTMLImageElement
type y = GetTypeIfConstructorDeclared<'ThingThatDoesNotExist'>; // y == never