我对这些课程有疑问。我想使用doSomething()类独有的方法B,而不是每次都对其a进行类型转换,但是当我将属性指定为 typeB时,它告诉我它没有在构造函数中分配,这有点错误,因为父构造函数执行分配。
class A {
}
class B extends A {
doSomething() { }
}
class One {
constructor(protected a: A){ }
}
class Two extends One {
protected a: B // Property 'a' has no initializer and is not definitely assigned in the constructor.
constructor(){
super(new B());
// If I enter "this.a = new B();" here then the error disappears, but the code is redundant.
}
doStuff() {
this.a.doSomething()
}
} …Run Code Online (Sandbox Code Playgroud) 有没有办法解决在类内部大量使用装饰器的问题?
这是我的NestJS应用程序中类的单个属性的示例,其中带有不完整的swagger文档装饰器:
@ApiModelProperty({
description: 'description',
})
@Expose()
@MaxLength(100, { message: 'message' })
@IsString({ message: 'message' })
@ValidateIf(address=> address.id !== null)
@NotEquals(undefined, { message: 'message' })
address: string;
Run Code Online (Sandbox Code Playgroud)
这很快就变得庞大而丑陋。有什么方法可以使代码看起来更简洁,在另一个文件中定义装饰器?