diff options
author | 2022-01-06 17:27:21 +0100 | |
---|---|---|
committer | 2022-01-06 10:27:21 -0600 | |
commit | 7e8f7c7e9ede3ea36797a4559597f6596da32284 (patch) | |
tree | 9af3926c46f47156d35323eaa18dd1b33d4d56f5 /docs/src | |
parent | 9a4c30989bd360133218d773f56f9ee792f118ba (diff) | |
download | astro-7e8f7c7e9ede3ea36797a4559597f6596da32284.tar.gz astro-7e8f7c7e9ede3ea36797a4559597f6596da32284.tar.zst astro-7e8f7c7e9ede3ea36797a4559597f6596da32284.zip |
Added "IntelliSense for TypeScript" (#2326)astro@0.22.7
Important knowledge to avoid type errors.
Diffstat (limited to 'docs/src')
-rw-r--r-- | docs/src/pages/en/guides/environment-variables.md | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/docs/src/pages/en/guides/environment-variables.md b/docs/src/pages/en/guides/environment-variables.md index 30ab36398..1d3b9cfef 100644 --- a/docs/src/pages/en/guides/environment-variables.md +++ b/docs/src/pages/en/guides/environment-variables.md @@ -29,3 +29,20 @@ fetch(`${import.meta.env.PUBLIC_POKEAPI}/pokemon/squirtle`); > ⚠️WARNING⚠️: > Because Vite statically replaces `import.meta.env`, you cannot access it with dynamic keys like `import.meta.env[key]`. + +## IntelliSense for TypeScript + +By default, Vite provides type definition for `import.meta.env` in `vite/client.d.ts`. While you can define more custom env variables in `.env.[mode]` files, you may want to get TypeScript IntelliSense for user-defined env variables which prefixed with `PUBLIC_`. + +To achieve, you can create an `env.d.ts` in `src` directory, then augment `ImportMetaEnv` like this: + +```ts +interface ImportMetaEnv { + readonly PUBLIC_POKEAPI: string + // more env variables... +} + +interface ImportMeta { + readonly env: ImportMetaEnv +} +``` |