I recently read an article on Hacker Noon about importing JSON into TypeScript, and decided to give it a try in my code. Here's the import line I used:
import data from './assets/fonts/helvetiker_bold.typeface.json';
To test if the import was working, I included a simple font JSON file:
{"hi":"true"}
Additionally, I checked my tsconfig.json file to ensure all necessary options were set:
{
"compilerOptions": {
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true,
"target": "es5",
"module":"es2015",
"lib": ["es2015", "es2016", "es2017", "dom"],
"strict": true,
"sourceMap": true,
"declaration": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declarationDir": "dist/types",
"outDir": "dist/lib",
"typeRoots": [
"node_modules/@types"
]
},
"include": [
"src"
]
}
Despite all this, I encountered an error during compilation:
[!] Error: Unexpected token
src\assets\fonts\helvetiker_bold.typeface.json (1:5)
1: {"hi":"true"}
^
I'm unsure what the issue is, so any assistance would be greatly appreciated.