简体   繁体   中英

How to define class with properties and its data type dynamically in typescript

For eg I have following class, with interface ProjectAttrs.

export class ProjectAttrsImpl implements ProjectAttrs {
    name: string;
    description: string | null;
    coordinates: string | null;   
}

I have too many classes in JSON implementation so trying to define all classes dynamically. I got following structure from JSON parse

 project: {
   name: 'string',
   description: 'string | null',
   coordinates: 'string | null',
  }

but its properties like name and description are taking value as string. I want to define its data-type as string not value. Please help

Parsing from typescript to JSON is not possible, it will not make an interface. A JSON file is just some text with a few primitives, including boolean, number, string and null.

A JSON parser is used from JSON to typescript to generate an interface based on the JSON provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM