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.
2022-09-13T21:14:21+03:00By Gandalf The White|Categories: How To|0 Comments
Share This Story, Choose Your Platform!
Related Posts
This site uses Akismet to reduce spam. Learn how your comment data is processed.