繁体   English   中英

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

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

例如,我有以下 class,接口为 ProjectAttrs。

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

我在 JSON 实现中有太多类,因此尝试动态定义所有类。 我从 JSON 解析得到以下结构

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

但它的名称和描述等属性将值作为字符串。 我想将其数据类型定义为字符串而不是值。 请帮忙

typescript解析JSON是不行的,不会做接口。 一个 JSON 文件只是一些带有一些原语的文本,包括 boolean、数字、字符串和 null。

JSON解析器JSONtypescript根据提供的JSON生成接口。

暂无
暂无

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

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