I have a node project with typescript and I'm trying to read the right url from a json object. However when using square bracket notation I get undefined.
{
"env":"dev",
"dev":{
"url": "url1"
},
"aws":{
"url": "url2"
}
}
For some reason this does not work:
const url = config[config.env].url;
The code below accomplishes what I need, but it is not as flexible as the above and I want to understand why it does not work the other way.
const cenv:string = config.env;
let url=null;
if( cenv === "dev")
url = config["dev"].url;
else
url = config["aws"].url;
This is my tsconfig file:
{
"compilerOptions": {
"lib": [
"es2017"
],
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"sourceMap": true,
"target": "es2017",
"outDir": "lib",
"resolveJsonModule": true
},
"exclude": [
"node_modules"
]
}
The json is imported to the project like so:
import * as config from './config.json';
