【问题标题】:Generate two classes with different decorator options生成两个具有不同装饰器选项的类
【发布时间】:2019-12-30 04:31:05
【问题描述】:

我正在寻找一种从这个类定义中创建两个类的方法。我需要两个版本的PlanInput,一个可以为真,一个为假。

const example = (nullable) => {

  @InputType()
  class PlanInput {

    @IsNotEmpty({ message: 'Plan name can\'t be empty' })
    @Trim()
    @Field({ nullable })
    name?: string

  }

  return PlanInput
}

export const PlanInput = example(true)
export const PlanCreateInput = example(false)

这在 typestack 系统中是可能的吗?还是单独在 es6 中?

【问题讨论】:

    标签: typescript typeorm typegraphql


    【解决方案1】:

    您可以通过扩展 PlanInput 并使用私有布尔值配置可空性来实现此目的。

    @InputType()
    class PlanInput {
      private nullable = true;
      @IsNotEmpty({ message: 'Plan name can\'t be empty' })
      @Trim()
      @Field({ nullable: this.nullable })
      name?: string
    }
    
    class PlanCreateInput extends PlanInput {
      private nullable = false;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-09
      • 2021-05-24
      • 2016-04-29
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      • 2020-03-04
      相关资源
      最近更新 更多