如何在typescript中实现具有静态属性的抽象类

如何在typescript中实现具有静态属性的抽象类,typescript,nestjs,typeorm,typedi,Typescript,Nestjs,Typeorm,Typedi,我正在创建一个作业系统,我希望我的DSL如下所示: @ScheduledTaskProcessor() 导出类ScoringProcessor扩展AbstractScheduledProcessor{ 静态cron=cron\u NIGHTLY\u第一批 异步进程(args:scoringput){ //做一些工作 } } 我希望AbstractScheduledProcessor如下所示: 导出抽象类AbstractScheduledProcessor{ 抽象静态cron:string; 抽

我正在创建一个作业系统,我希望我的DSL如下所示:

@ScheduledTaskProcessor()
导出类ScoringProcessor扩展AbstractScheduledProcessor{
静态cron=cron\u NIGHTLY\u第一批
异步进程(args:scoringput){
//做一些工作
}
}
我希望AbstractScheduledProcessor如下所示:

导出抽象类AbstractScheduledProcessor{
抽象静态cron:string;
抽象过程(args:T):承诺
…其他非抽象方法如下。。。
但我得到:TS1243:“静态”修饰符不能与“抽象”修饰符一起使用

任何人都可以提出一条前进的道路。也许我可以使用我的类装饰器作为HOF来创建具有静态属性的类。

仅供参考,我的ScheduledTaskProcessor decorator函数目前如下所示:

// This interface represents the type of an uninstantiated class, so the class itself.
// In Javascript it's just regularly called constructor but you could also call 
// it SchedulerClass
interface SchedulerConstructor { 
  cron: string; // A member of the uninstantiated class/constructor is static.
  new (...args: any[]): any;
}

// Decorators get the class itself passed not an instantiation of it. This means interfaces
// we set here define the structure of the class and not of the object. This gives us
// the opportunity to use our constructor-interface to restrict which classes can be
// decorated and thus enforcing the static member cron.
export function ScheduledTaskProcessor(constructor: SchedulerConstructor) {
    // decorator logic
}

@ScheduledTaskProcessor // No compiler warning because the static is set.
class ScoringProcess {
    static cron = "test"
}

@ScheduledTaskProcessor // Error static cron not declared.
class ScoringProcessError {
}
从'typedi'导入{Container}
导出函数ScheduledTaskProcessor():ClassDecorator{
返回函数(目标){
console.log('注册:',目标)
Container.of('ScheduledTaskProcessor').set(target.name,target)
}
}

您可以确保必须使用装饰器设置静态值。方法如下所示:

// This interface represents the type of an uninstantiated class, so the class itself.
// In Javascript it's just regularly called constructor but you could also call 
// it SchedulerClass
interface SchedulerConstructor { 
  cron: string; // A member of the uninstantiated class/constructor is static.
  new (...args: any[]): any;
}

// Decorators get the class itself passed not an instantiation of it. This means interfaces
// we set here define the structure of the class and not of the object. This gives us
// the opportunity to use our constructor-interface to restrict which classes can be
// decorated and thus enforcing the static member cron.
export function ScheduledTaskProcessor(constructor: SchedulerConstructor) {
    // decorator logic
}

@ScheduledTaskProcessor // No compiler warning because the static is set.
class ScoringProcess {
    static cron = "test"
}

@ScheduledTaskProcessor // Error static cron not declared.
class ScoringProcessError {
}

错误解释了问题。要么将属性设置为抽象属性,要么设置为静态属性。如前所述,为什么不将cron字符串设置为静态并在需要时覆盖它呢?太棒了!做得太好了Mirco S!感谢和平ot,欢迎访问