typescript json file code example
Example 1: Cannot find module './data.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
add to tsconfig.json
{
...
"resolveJsonModule": true
}
}
Example 2: read json file in typescript
{
"greeting" : "xin chao Vietnam"
}
import * as data from 'data.json';
let greeting = data.greeting;
"compilerOptions": {
....
"resolveJsonModule": true,
....
},
Example 3: typescript how to mode json files when compile
{
"compilerOptions": {
"resolveJsonModule": true,
},
"include": [
"*/src/**/package.json"
]
}
Example 4: typescript read json file
"resolveJsonModule": true,
"esModuleInterop": true,
Example 5: how to import a json string from a file in typescript
import {default as a} from "a.json";
a.primaryMain