How to type json files as tsconfig.json and package.json

256 views Asked by At

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?

2

There are 2 answers

0
mindlid On BEST ANSWER

I already found that the json Schemas are based on the json-schema.org that would enable intellisense for json files

here is a good intro for defining your own json schema https://json-schema.org/understanding-json-schema/basics.html

here is a screenshot enter image description here

2
Mina Ragaie On

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