[SOLVED] Creating typescript interface from JSON object


Is there a way to import a JSON object into a Typescript program so that it is automatically strongly typed and constant?

So if I have this code in person.json:

  "name": "Steven",
  "eyeColor": "brown"

I want to be able to import it to a Typescript program as such:

// personTest.ts
const person = require('person.json');
console.log(person.name); // This should be fine
console.log(person.age);  // This should cause a compile-time error, because this property is not defined


add this on tsconfig.json


   ... //prev code

   "resolveJsonModule": true, 


then you can use ESM modules to import json file and it will be auto typed the object values

import person from './person.json' //be sure from the json path

Answered By – Chemi Adel

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *