[英]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.