【发布时间】:2020-05-02 17:18:38
【问题描述】:
我正在使用 typegoose 来定义我的 mongoose/mongo-db 模型。 typegoose 模型可能看起来像
class Something {
@prop({ required: true }) // this is now required in the schema
public firstName!: string;
@prop() // by default, a property is not required
public lastName?: string; // using the "?" marks the property as optional
}
(来自https://typegoose.github.io/typegoose/docs/decorators/prop/)
我想重用这些类来获得各种打字稿接口,例如api 调用。
我将如何剥离所有 @prop(...)-lines 并生成看起来像的东西
interface Something {
firstName: string,
lastName?: string
}
我想我想使用https://www.typescriptlang.org/docs/handbook/advanced-types.html#mapped-types,但我不确定。
【问题讨论】:
-
那个装饰器不应该影响 TS 编译时类型。例如,您可以通过
interface SomethingI extends Something { }从Something创建一个接口,然后像type T1 = keyof SomethingI // "firstName" | "lastName"一样使用它
标签: typescript typegoose