Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors when using types with a web worker. #151

Open
jozefchutka opened this issue Jun 19, 2024 · 4 comments
Open

Errors when using types with a web worker. #151

jozefchutka opened this issue Jun 19, 2024 · 4 comments

Comments

@jozefchutka
Copy link

Following similar issue to #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
Copy link
Collaborator

kainino0x commented Jun 20, 2024

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
Copy link
Author

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
Copy link
Collaborator

kainino0x commented Jun 21, 2024

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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants