-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
importMetaEnv doesn't work. #3524
Comments
Could you try interface ImportMetaEnv {
readonly VITE_API_TIMEOUT: number;
readonly VITE_MOCKUP: boolean;
}
interface ImportMeta {
readonly env: ImportMetaEnv;
} |
@Shinigami92 Tried the way you gave. |
Can't reproduce. It works on my machine. |
Related to #3214 and docs on client-types. You have two options:
PR #3214 just migrated the |
I don't understand, how to solve it? |
@mrerhuo
/// <reference types="vite/client" />
interface ImportMetaEnv {
YOUR_PROP: string;
} then you can use it like this |
I don't think the types are converted by default. You have to do it yourself.I think the Anyway, the solution is to convert the types by yourself. If you want some inspiration for now, I use it like this in my project: const parseBoolean = (value: string | boolean | undefined, defaultValue: boolean) => {
if (typeof value === 'undefined') {
return defaultValue;
}
if (typeof value === 'boolean') {
return value;
}
switch (value.toLowerCase().trim()) {
case "true": case "yes": case "1": return true;
case "false": case "no": case "0": return false;
default: return defaultValue;
}
}
const Config = {
isDev: import.meta.env.DEV,
firebaseConfig: {
apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
databaseURL: import.meta.env.VITE_FIREBASE_DATABASE_URL,
projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,
appId: import.meta.env.VITE_FIREBASE_APP_ID,
measurementId: import.meta.env.VITE_FIREBASE_MEASUREMENT_ID,
},
useEmulators: parseBoolean(import.meta.env.VITE_USE_EMULATORS, import.meta.env.DEV),
};
export default Config;
interface ImportMetaEnv {
VITE_FIREBASE_API_KEY: string | undefined;
VITE_FIREBASE_AUTH_DOMAIN: string | undefined;
VITE_FIREBASE_DATABASE_URL: string | undefined;
VITE_FIREBASE_PROJECT_ID: string | undefined;
VITE_FIREBASE_STORAGE_BUCKET: string | undefined;
VITE_FIREBASE_MESSAGING_SENDER_ID: string | undefined;
VITE_FIREBASE_APP_ID: string | undefined;
VITE_FIREBASE_MEASUREMENT_ID: string | undefined;
VITE_USE_EMULATORS: string | undefined;
VITE_WEBSITE_URL: string | undefined;
VITE_APP_LOCALHOST_PORT: string | undefined;
VITE_WEBSITE_LOCALHOST_PORT: string | undefined;
} My code base imports |
Recommended in docs way to add types causes build problem: interface ImportMetaEnv extends Readonly<Record<string, string>> {
readonly VITE_VAR: string
}
interface ImportMeta {
readonly env: ImportMetaEnv
}
This works interface ImportMetaEnv extends Readonly<Record<string, string | boolean | undefined>> {
readonly VITE_VAR: string
}
interface ImportMeta {
readonly env: ImportMetaEnv
} |
@imShara I can confirm this 👍 Correct would be without /// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_VAR: string
}
interface ImportMeta {
readonly env: ImportMetaEnv;
} |
@Shinigami92, I'm newbie in typescrypted vue, can you explain your suggestion? |
My suggestion would be to open a new PR and update the documentation with exactly my example above 🙂 |
@Shinigami92, I mean why your version correct, not mine? Why you drop |
Maybe |
No, it not works, because record's value type can be |
As far as I know, only the non |
But |
You referring to this? #4194 |
``` node_modules/.pnpm/[email protected]/node_modules/vite/types/importMeta.d.ts:62:11 - error TS2430: Interface 'ImportMetaEnv' incorrectly extends interface 'Readonly<Record<string, string>>'. 'string' index signatures are incompatible. Type 'string | boolean | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'. 62 interface ImportMetaEnv { ~~~~~~~~~~~~~ ``` More info in this thread vitejs#3524 (comment)
Hi, I cannot access my environment variable prefixed properly "VITE_APP_TITLE" inside my code. I try to access it with "import.meta.env.VITE_APP_TITLE" in typescript. Do you have any idea about it?
|
@jromerooo2 thank you for the resolution. The vitejs guide tells that |
@erdemtuna I used "?" on the env variable name. As mentioned here: https://stackoverflow.com/questions/66039933/typescript-types-for-import-meta-env |
Is this still an issue? It works for me following https://vitejs.dev/guide/env-and-mode.html#intellisense-for-typescript |
Closing as the documentation has been updated with the correct way of typing |
Describe the bug
I used the intellisense function as per the guide, but it doesn't work.
/src/env.d.ts
.env
And, when import.meta.env is printed from main.ts to the console...
Intellisense does not work for .env settings. It only comes in string type.
Reproduction
I want it to be converted to the type applied in intellisense (
env.d.ts
).System Info
package.json
Used package manager:
pnpm
The text was updated successfully, but these errors were encountered: