从打字稿中的对象生成界面

发布于 2025-01-25 21:35:15 字数 1531 浏览 3 评论 0原文

我有一个typescript项目,我想从object中的数据中生成接口,我不知道是否这样是可能的,但我做不到。

我要做的就是以下内容:

  1. 我从该对象的值中获得了json数据库对象,
  2. 我需要创建一个动态界面,以在每个值中添加一个arraylist,并且因此,避免每次数据库值更改时修改界面

,这是对象我从数据库中获得的:

interface Auto {
  autoType: string
}
let autoActive: Auto[] = [
  {  "autoType": "Car"},
  {  "autoType": "Motorcycle"},
  {  "autoType": "Truck"},
  {  "autoType": "Plane"}
]

console.log(autoActive.map(Object.values).toString()); // Car,Motorcycle,Truck,Plane

这是接口我需要从上面的数据创建(> > Autobrand)来自genericObject模型:

interface genericObject {
  [key: string]: any;
}
interface AutoBrand extends genericObject {
  "Car": Array<string>,
  "Motorcycle": Array<string>,
  "Truck": Array<string>,
  "Plane": Array<string>
}

我想从中获得的是生成常数对象将标题分配给每个最终文件,这是一个我想获得的最后一堂课的示例,然后从这里提取标题:

export class HeaderConst {
  static readonly headerConst: AutoBrand = {
    "Car": [
      "Model",
      "Total",
      "Age"
    ],
    "Motorcycle": [
      "Model",
      "Total",
      "Age"
    ],
    "Truck": [
      "Model",
      "Total",
      "Age"
    ],
    "Plane": [
      "Model",
      "Total",
      "Age"
    ]
  }
  public static getConstants(elem: string) {
    return elem.split(".").reduce((elm: any, i: any) => {
        return elm[i];
    }, this.headerConst);
  }
}

I have a Typescript project in which I want to generate an Interface from the data I have in an object, I don't know if this is possible but I can't do it.

What I do is the following:

  1. I get the JSON Database Object with the results of a query
  2. From the values of that object I need to create a dynamic interface to add an ArrayList to each of those values, and thus avoid modifying the interface every time the database values change

This is the object I get from database:

interface Auto {
  autoType: string
}
let autoActive: Auto[] = [
  {  "autoType": "Car"},
  {  "autoType": "Motorcycle"},
  {  "autoType": "Truck"},
  {  "autoType": "Plane"}
]

console.log(autoActive.map(Object.values).toString()); // Car,Motorcycle,Truck,Plane

This is the interface I need to create from the above data (AutoBrand) from the genericObject model:

interface genericObject {
  [key: string]: any;
}
interface AutoBrand extends genericObject {
  "Car": Array<string>,
  "Motorcycle": Array<string>,
  "Truck": Array<string>,
  "Plane": Array<string>
}

What I want to obtain from this is to generate a constant object to assign the titles to each final file, this is an example of the final class that I want to obtain, and then extract the titles from here:

export class HeaderConst {
  static readonly headerConst: AutoBrand = {
    "Car": [
      "Model",
      "Total",
      "Age"
    ],
    "Motorcycle": [
      "Model",
      "Total",
      "Age"
    ],
    "Truck": [
      "Model",
      "Total",
      "Age"
    ],
    "Plane": [
      "Model",
      "Total",
      "Age"
    ]
  }
  public static getConstants(elem: string) {
    return elem.split(".").reduce((elm: any, i: any) => {
        return elm[i];
    }, this.headerConst);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若有似无的小暗淡 2025-02-01 21:35:15

那是不可能的。您的JSON仅在运行时存在,并且在运行时根本不存在打字稿接口。

That's not possible. Your JSON only exists at runtime, and TypeScript interfaces do not exist at runtime at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文