MatthiasKunnen / nest-config-service-optional-env-var

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problems: Nestjs config service with optional environment variables

Return value from ConfigService.get is string undefined

When the validate function returns an object where a key, e.g. OPT, is undefined, assignVariablesToProcess will assign undefined to process.env.OPT which will stringify it (see https://nodejs.org/api/process.html#processenv). This results in ConfigService.get returning process.env.OPT which now has the value 'undefined'.

Reproduce

yarn run start and observe the log.

Return type from ConfigService<EnvironmentVariables, true>.get excludes undefined

The second generic parameter (WasValidated) changes the return value of ConfigService.get. If true, it removes undefined from the return type. If false, it adds undefined. The problem here is that a value could be both validated and undefined.

Reproduce

Observe the TypeScript return type of optValue in main.ts.

About


Languages

Language:TypeScript 100.0%