I am new in typescript and I have 2 questions.
a. I want to import a simple JSON file in my typescript (main.ts) using the following code in visual studio code (3.6.2) editor:
import test from "./test.json";
console.log(test.name);
I also tried with import * as test from "./test.json";
While the JSON file (test.json) is the following:
{"name": "testing",
"age":25}
However, when I am typing "test.", typescript is giving the options for "name", "age" as the properties.
And the tsconfig.json file is following:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true
}
}
This code is throwing the following error:
Cannot find module './test.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
Since the type definition is for old versions, I haven't used that. My typescript version is 3.6.3. The above mentioned 3 files are inside the same folder.
-testfolder
- main.ts
- tsconfig.json
- test.json
Though there are some other questions similar to this, but I couldn't find the problem in my code! I have also tested that with submile text 3 but it's also giving the same error!
b. If I want to parse a json data from a file where the file extension is different (not .json), how should I do that?
Thank you in advance for your time.