Errors when using types with a web worker.
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 {}
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.
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.
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.
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