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

vite config files can't be compiled without dom types #9813

Open
7 tasks done
DetachHead opened this issue Aug 24, 2022 · 3 comments
Open
7 tasks done

vite config files can't be compiled without dom types #9813

DetachHead opened this issue Aug 24, 2022 · 3 comments

Comments

@DetachHead
Copy link

Describe the bug

when attempting to compile a vite.config.ts file without dom types, and without skipLibCheck in tsconfig.json, the following errors occur:

❯ tsc
node_modules/esbuild/lib/main.d.ts:592:16 - error TS2503: Cannot find namespace 'WebAssembly'.

592   wasmModule?: WebAssembly.Module
                   ~~~~~~~~~~~

node_modules/vite/dist/node/index.d.ts:1259:13 - error TS2304: Cannot find name 'Worker'.

1259     worker: Worker
                 ~~~~~~


Found 2 errors in 2 files.

Errors  Files
     1  node_modules/esbuild/lib/main.d.ts:592
     1  node_modules/vite/dist/node/index.d.ts:1259

i believe it's because these types are defined globally in lib.dom.d.ts. but since the vite.config.ts file is executed by nodejs, it's incorrect to include the dom types when compiling it.

is it possible to resolve this by importing the Worker type from elsewhere?

Reproduction

https://stackblitz.com/edit/vitejs-vite-ghpgs3?file=vite.config.ts,tsconfig.json,package.json&terminal=dev

System Info

❯ npx envinfo --system --npmPackages '{vite,@vitejs/*}' --binaries --browsers
ERR!: Failed to install package

error Command failed with exit code 1

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red
Copy link
Member

I think adding the following code will work.

declare global {
  interface Worker {}

  namespace WebAssembly {
    interface Module {}
  }
}

@DetachHead
Copy link
Author

this issue also seems to be present in the opposite scenario. when using vite types in the browser, it relies on @types/node. see sveltejs/kit#9348 (comment)

@christian-hackyourshack

I can confirm, that I cannot typecheck my project with tsc, because it uses vitest (but not vite): It complains about Worker, WebSocket and WebAssembly.Module not being defined (in some node_module/vite/... files).

My project(s) are in a monorepo with npm. Excluding node_modules in my tsconfig.json did not help.

A workaround that helped me, was to include: ["vitest.d.ts"] in my tsconfig.json with the following content

// vitest.d.ts
declare interface Worker {}
declare interface WebSocket {}

declare namespace WebAssembly {
  interface Module {}
}

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

No branches or pull requests

3 participants