如何键入一个模型以使其像环回中的对象一样工作?
How do I type a model to act like an object in loopback?
我有一个环回模型(在许多情况下)以其自身的原始 json 形式表示。例如
@model()
class SomeModel extends Entity {
@property({ type: 'string' })
id?: string;
}
...原始 json 将是
interface IRawSomeModel {id?: string}
有没有办法以编程方式获取 IRawSomeModel
?
我能想到的一种方法是将两者结合起来,但是需要重复很多额外的工作,例如
export interface IRawSomeModel {id?: string}
@model()
export class SomeModel extends Entity implements IRawSomeModel {
@property({ type: 'string' })
id?: string;
}
最终,我正在寻找的是符合 RawObjectFormOfModel<SomeModel>
语法的东西
所有这一切的重点是能够拥有这样的代码:
const obj: RawObjectFormOfModel<SomeModel> = {}; // no error about missing class functions
obj.id = "test"
获取模型的原始对象类型表示的最佳方法是什么?
lb4 中的实体 class 中有两种方法可用。 toJSON() 和 toObject()。它们都 return 模型 class 的普通对象表示。您可以将它们用于此目的。
来自dts定义"model.d.ts"
/**
* Serialize into a plain JSON object
*/
toJSON(): Object;
/**
* Convert to a plain object as DTO
*/
toObject(options?: Options): Object;
我能找到的最好的方法是创建一个接口并实现它。
export interface IRawSomeModel {id?: string}
@model()
export class SomeModel extends Entity implements IRawSomeModel {
@property({ type: 'string' })
id?: string;
}
我希望我能找到一种方法来获取装饰器信息并使用它来生成界面。
我有一个环回模型(在许多情况下)以其自身的原始 json 形式表示。例如
@model()
class SomeModel extends Entity {
@property({ type: 'string' })
id?: string;
}
...原始 json 将是
interface IRawSomeModel {id?: string}
有没有办法以编程方式获取 IRawSomeModel
?
我能想到的一种方法是将两者结合起来,但是需要重复很多额外的工作,例如
export interface IRawSomeModel {id?: string}
@model()
export class SomeModel extends Entity implements IRawSomeModel {
@property({ type: 'string' })
id?: string;
}
最终,我正在寻找的是符合 RawObjectFormOfModel<SomeModel>
所有这一切的重点是能够拥有这样的代码:
const obj: RawObjectFormOfModel<SomeModel> = {}; // no error about missing class functions
obj.id = "test"
获取模型的原始对象类型表示的最佳方法是什么?
lb4 中的实体 class 中有两种方法可用。 toJSON() 和 toObject()。它们都 return 模型 class 的普通对象表示。您可以将它们用于此目的。
来自dts定义"model.d.ts"
/**
* Serialize into a plain JSON object
*/
toJSON(): Object;
/**
* Convert to a plain object as DTO
*/
toObject(options?: Options): Object;
我能找到的最好的方法是创建一个接口并实现它。
export interface IRawSomeModel {id?: string}
@model()
export class SomeModel extends Entity implements IRawSomeModel {
@property({ type: 'string' })
id?: string;
}
我希望我能找到一种方法来获取装饰器信息并使用它来生成界面。