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
import env from '@/env'
export function url( envName: string): string
{
console.log(env[envName])
// Also does not work with env.[envName]
...
typeof envName: string
results in:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ NODE_ENV: "development" | "production"; ...
No index signature with a parameter of type 'string' was found on type '{ NODE_ENV: "development" | "production ...";
I don't really understand those error messages. Maybe someone could help me?
If not, is there another way to pass an env name to a function and use that to read the env value?
(e.g. call myFunc('URL_PRODUCTION') -> myFunc reads env.URL_PRODUCTION as env.[PARAM_NAME] / env[PARAM_NAME])
btw: reading process.env[envName] (bypassing envalid) works without issues
The text was updated successfully, but these errors were encountered:
Instead of typing envName as string could you try envName: keyof typeof env? I think that should do the trick, typescript is rightfully complaining because not just any string will be a valid key of the cleaned env object. Hope that helps!
I'm trying to read a variable from the env object dynamically using a string variable as the property name, but I can't.
env.ts
typeof
env
:object
url.ts
typeof
envName
:string
results in:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ NODE_ENV: "development" | "production"; ...
No index signature with a parameter of type 'string' was found on type '{ NODE_ENV: "development" | "production ...";
I don't really understand those error messages. Maybe someone could help me?
If not, is there another way to pass an env name to a function and use that to read the env value?
(e.g. call
myFunc('URL_PRODUCTION')
-> myFunc readsenv.URL_PRODUCTION
asenv.[PARAM_NAME] / env[PARAM_NAME]
)btw: reading
process.env[envName]
(bypassing envalid) works without issuesThe text was updated successfully, but these errors were encountered: