In this example, I will show you a simple way to import JSON files in TypeScript code. It requires just two simple steps. The first is to change the tsconfig.json file by adding the line:
, "resolveJsonModule": true
in “compilerOptions” section.
{
"compilerOptions":
{
...
, "resolveJsonModule": true
...
}
...
}
After that, you have to import the file into your ts-file.
import * as config from './config.json';
Now we are ready. We can use it the following way:
import * as config from './config.json';
interface AppConfig{
prop: string;
}
class App{
...
public configuration: AppConfig;
private constructor(){
this.configuration = config as AppConfig; //just for example
}
...
}
Was this article helpful?
If you have any suggestions or questions, please leave a comment below.
Leave A Comment