Typescript Types for import.meta.env Typescript Types for import.meta.env typescript typescript

Typescript Types for import.meta.env


It's documented here: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html#support-for-importmeta

So you're almost there:

interface ImportMeta {  env: {    GITHUB_AUTH_TOKEN: string;    NODE_ENV: 'development' | 'production';    PORT?: string;    PWD: string;  };}

But be aware that in vite all environment variables have to be prefixed with VITE_ so neither of those environment variables are available in the application.


I had similar problems and solved it by

tsconfig.json

{  ...  "compilerOptions": {    ...    "target": "ESNext",    "types": ["vite/client"]  }}

Have vite installed as a dev dependency.


I got it to work by using the following -

interface ImportMetaEnv {  VITE_PORT?: string;  VITE_AUTH_TOKEN?: string;}