I have a file called men.json.
I want to do the equivalent of var men = require('./men.json');
.
Whatever I have tried it looks for ./men.json.js
file.
I read that I can not use import
since it is not a ts
file.
What is the equivalent required line?
The TypeScript comes up with the functionality of working with JSON Type data. JSON being the JavaScript Object Notation, is used to make a data model that is easy to write and read. We can easily analyze large and complex data set with this TypeScript JSON type.
Use the JSON. parse() method to parse a JSON string in TypeScript, e.g. const result: Person = JSON. parse(jsonStr) . The method parses a JSON string and returns the corresponding value.
declare module '*.json' {
var _: any;
export default _;
}
then you can import mem from './mem.josn'
put this into some file that's included in tsconfig.json
now you can require .json
, this works for any other file formats (though you need webpack since nodejs cannot require them directly)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With