Home > Software design >  How to type json files as tsconfig.json and package.json
How to type json files as tsconfig.json and package.json

Time:11-25

I noticed that some json files with specific names get intellisense, like:

  1. tsconfig.json
  2. jsconfig.json
  3. package.json
  4. .eslintrc.json
  5. .prettierrc.json

Some screenchots

[enter image description here]

[enter image description here]

how can i type the schema of my own json files is it declares as a json module or something similar?

CodePudding user response:

just create a person.model.ts file that carries your json interface then export the interface from the file in which is defined

interface Person{
  name: string;
  age: number;
}

then import it wherever you want to use it

import Person from "./person.model.ts"

then use it as a type for your json

 let p:Person = {
  name: 'Joe',
  age: 20
}

also your editor/IDE will guide you with the missing types and will throw an error if you entered a wrong property like that enter image description here

CodePudding user response:

I already found that the json Schemas are based on the enter image description here

  • Related