【问题标题】:Remove property/class member decorators with mapped types删除具有映射类型的属性/类成员装饰器
【发布时间】: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


【解决方案1】:

正如@ford04 已经说过的,当你想将它与new 一起使用或作为类型使用时,typegoose 的装饰器不会更改类

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 2018-09-14
    • 2017-04-04
    • 2020-05-01
    • 1970-01-01
    • 2020-06-20
    • 2012-09-23
    • 1970-01-01
    相关资源
    最近更新 更多