8000 Access value of env object by name · Issue #191 · af/envalid · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Access value of env object by name #191
Closed
@sephentos

Description

@sephentos

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0