-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect types for values using environment variables #82
Comments
As a sidenote: |
I would like to propose something completely different here — |
It would work something like this import {config, Source} from 'node-config-ts'
type Color = "RED" | "BLUE" | "GREEN"
type Config = {
port: number,
color: Color
}
// Generates a config with some default values
const auto: Source<Config> = config.automatic.as<Config>()
// Reads from default.json
const default: Source<Config> = config.file("./default.json").as<Config>()
// Reads from production.json
const production: Source<Config> = config.file("./production.json").as<Config>()
// Read from env variables
const env: Source<Config> = config.env.as<Config>()
// Merge all the configurations
const merged: Source<Config> = auto.orElse(default).orElse(production).orElse(env)
console.log(await merged.generate()) // Will print the final config |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe the bug:
Types generated for properties using environment variables are incorrect. Environment variable configs should use
string | undefined
as that's what@types/node
uses for environment variables. However, during interface generation, each environment variable is eitherstring
orundefined
, depending on whether or not it's defined at the time. When it is, the property will have a type ofstring
. Otherwise, it will have a type ofundefined
.Suspected Cause
Type generation follows the following process:
json-to-ts
e.g.
When
process.env.FOO
is undefined,mergeAllConfigs
results in the final object:json-to-ts
, seeingundefined
, resolves the type for that particular config toundefined
.Possible Solutions
Solution 1: use
null
instead ofundefined
(worse solution)json-to-ts
handlesnull
values, but notundefined
values, by making their types optional andany
. The below test from their codebase demonstrates this behavior.This is better than
undefined
as this typing is correct, but it's not very specific.Solution 2: use optional property name syntax
json-to-ts
has undocumented behavior for creating optional types based on property names. Its implemented in this function. Properties ending in--?
will have optional types, and the--?
suffix gets removed from the resulting type. e.g.Turns into
We could use this to our advantage. During type generation, we could insert dummy values into values that use environment variables and add the
--?
suffix to the key. This would result in the correct type. The downside is that this solution relies on undocumented behavior.Steps To Reproduce:
Create a bare-bones default config
Do not set a value for
FOO
in your terminal. Then, run the CLI. The following type declaration will be created:Next, set foo and run the CLI again.
export FOO=foo node ./bin/cli
The following type declaration will be created:
Expected behavior:
The type declaration below should be generated:
Typescript Version:
Tested on
3.7.5
and4.9.4
. Should not affect the behavior.node-config-ts version:
3.1.0
and3.2.0
The text was updated successfully, but these errors were encountered: