簡體   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