简体   繁体   English

如何在typescript中动态定义class的属性及其数据类型

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

For eg I have following class, with interface ProjectAttrs.例如,我有以下 class,接口为 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.我在 JSON 实现中有太多类,因此尝试动态定义所有类。 I got following structure from JSON parse我从 JSON 解析得到以下结构

 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.typescript解析JSON是不行的,不会做接口。 A JSON file is just some text with a few primitives, including boolean, number, string and null.一个 JSON 文件只是一些带有一些原语的文本,包括 boolean、数字、字符串和 null。

A JSON parser is used from JSON to typescript to generate an interface based on the JSON provided. JSON解析器JSONtypescript根据提供的JSON生成接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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