devsner
devsner

Reputation: 399

How can I type to typing to JSON file for typescript?

I have a JSON file. In this JSON file has a image data. (not a package.json or tsconfig.json etc)

I must have used this file for image rendering to web browser. But, This JSON is in the parent object like ( the example code is sample for question )

let a = {'b': 3, 'c':5, 'd':'string', 'imageJson': *json object* }

So, I have typed

interface A {
  b: number
  c:number
  d:string 
  imageJson : JSON     ** The error happened.
}

was failed with the error code below ( the error code is actual code which I have shown )

Type '{ v: string; fr: number; ip: number; op: number; w: number; h: number; nm: string; 
ddd: number; assets: ({ id: string; layers: ({ ddd: number; ind: number; ty: number; nm: string; 
refId: string; sr: number; ks: { o: { ...; }; r: { ...; }; p: { ...; }; a: { ...; }; s: { ...; }; }; 
... 8 more ...; shapes?: undefined; ...' is missing the following properties 
from type 'JSON': parse, stringify, [Symbol.toStringTag]  TS2739

What could I fix the code?

Upvotes: 1

Views: 208

Answers (1)

theRealEmu
theRealEmu

Reputation: 1504

As of TS 3.7 you can try this recursive declaration:

type Json = string | number | boolean | null | Json[] | { [key: string]: Json };

More at: https://www.typescriptlang.org/play#example/recursive-type-references

Upvotes: 1

Related Questions