[英]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解析器从JSON到typescript根据提供的JSON生成接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.