Description
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
import { bool, cleanEnv, port, str } from 'envalid'
export default cleanEnv( process.env, {
NODE_ENV: str( { choices: ['development', 'production'] } ),
...
} )
typeof env
: object
url.ts
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