You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The loadEnv method currently declares its return type as Record<string, string>.
However, in practice, it returns undefined if we try to get env variable with key that doesn't exist.
import{loadEnv}from'vite';constenv=loadEnv('','./');constfoo=env['XYZ'];// ts type inference marks `foo` as 'string'console.log('foo value:',foo);// the value is actually 'undefined'
This assumption that all keys are always string might lead to potential runtime errors. Those errors could simply be avoided by forcing the if (value !== undefined) check with proper return type in loadEnv.
Proposed Fix:
Update the return type of loadEnv to Record<string, string | undefined> or Partial<Record<string, string>>.
I'm willing to submit a PR for this issue if solution is agreed on.
There is a similar issue, but for some reason the original author decided to close it after being asked to present an example and didn't describe the issue properly. #18643
Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
This is how TypeScript works. You can enable that behavior by setting "noUncheckedIndexedAccess": true. I think Vite should keep the types as-is and let the user decide how they want to deal with it.
Describe the bug
The
loadEnv
method currently declares its return type asRecord<string, string>
.However, in practice, it returns
undefined
if we try to get env variable with key that doesn't exist.This assumption that all keys are always
string
might lead to potential runtime errors. Those errors could simply be avoided by forcing theif (value !== undefined)
check with proper return type inloadEnv
.Proposed Fix:
Update the return type of
loadEnv
toRecord<string, string | undefined>
orPartial<Record<string, string>>
.I'm willing to submit a PR for this issue if solution is agreed on.
There is a similar issue, but for some reason the original author decided to close it after being asked to present an example and didn't describe the issue properly.
#18643
Reproduction
https://stackblitz.com/edit/vitejs-vite-qjnysz6r?file=vite.config.ts
Steps to reproduce
npm install && npm run dev
vite.config.ts
and look at console output fromdev
commandValidations
The text was updated successfully, but these errors were encountered: