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
In some situations, we may want the Config class to throw an error if a configuration value is not specified. For example, when calling Config.get with "mongodb.uri", we probably want to throw an error if we did not provide the database URI.
2. Strict type checking
The Config.get method coerces the configuration value when possible (for example 'true' becomes true). But it does not guarantee that the type of the configuration value is correct.
For example with the below configuration, Config.get<boolean>('settings.debug') returns a string whereas its TypeScript type is boolean.
settings:
debug: 'an incorrect value'
Solution
Add two new methods getOrThrow and get2:
// If the config value is true, then return true.// If the config value is false, then return false.// If the config value is "true", then return true.// If the config value is "false", then return false.// If the config value is "xxx", then throw an error.// If the config value is not defined, then return the default value (true).config.get2('settings.foobar','boolean',true): boolean;// Same as above except that if the value is not defined, the method throws an error.config.getOrThrow('settings.foobar','boolean'): boolean;// Same as above except that if the value is not defined, the method returns undefined.config.get2('settings.foobar','boolean'): boolean |undefined;// Same with numbersconfig.get2('settings.foobar','number',3): number;config.getOrThrow('settings.foobar','number'): number;config.get2('settings.foobar','number'): number |undefined;// Same with stringsconfig.get2('settings.foobar','string','a'): string;config.getOrThrow('settings.foobar','string'): string;config.get2('settings.foobar','string'): string |undefined;// "Same" with anyconfig.get2('settings.foobar','any','a'): any;config.getOrThrow('settings.foobar','any'): any;config.get2('settings.foobar','any'): any;
The text was updated successfully, but these errors were encountered:
Issues
1. Empty config values
In some situations, we may want the
Config
class to throw an error if a configuration value is not specified. For example, when callingConfig.get
with"mongodb.uri"
, we probably want to throw an error if we did not provide the database URI.2. Strict type checking
The
Config.get
method coerces the configuration value when possible (for example'true'
becomestrue
). But it does not guarantee that the type of the configuration value is correct.For example with the below configuration,
Config.get<boolean>('settings.debug')
returns a string whereas its TypeScript type isboolean
.Solution
Add two new methods
getOrThrow
andget2
:The text was updated successfully, but these errors were encountered: