从打字稿中的对象生成界面
我有一个typescript
项目,我想从object
中的数据中生成接口
,我不知道是否这样是可能的,但我做不到。
我要做的就是以下内容:
- 我从该对象的值中获得了
json
数据库对象, - 我需要创建一个动态界面,以在每个值中添加一个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:
- I get the
JSON
Database Object with the results of a query - 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是不可能的。您的JSON仅在运行时存在,并且在运行时根本不存在打字稿接口。
That's not possible. Your JSON only exists at runtime, and TypeScript interfaces do not exist at runtime at all.