简体   繁体   中英

Generate two classes with different decorator options

I am looking for a way to create two classes from this one class definition. I need two versions of PlanInput , one where nullable is true, and one where it's false.

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)

Is this possible within the typestack system? Or within es6 alone?

You can achieve this by just extending PlanInput and configure nullability using a private boolean.

@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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM