Exploring the use of typescript for implementing type checking on a configuration file similar to the one below:
const _config = {
local: {
host: 'localhost',
port: 1234
},
dev: {
host: 'https://dev.myapp.com',
port: 80
},
prod: {
host: 'https://prod.myapp.com',
port: 80
},
}
export const config = _config[process.env.NODE_ENV || 'dev'];
I aim to define an interface for the nested objects to ensure that all configuration properties are present at compile time, rather than runtime:
interface IConfig {
host: string;
port: number;
}
const _config = {
local: { // <-- I want to specify this object as type IConfig
host: 'localhost',
port: 1234
},
dev: { // <-- I want to specify this object as type IConfig
host: 'https://dev.myapp.com',
port: 80
},
prod: { // <-- I want to specify this object as type IConfig
host: 'https://prod.myapp.com',
port: 80
},
}
export const config = _config[process.env.NODE_ENV || 'dev'];
Is this achievable? Are there any alternative approaches to accomplish the same objective?