有人可以提供一个简单的例子来演示实现装饰器的正确方法,并描述可能的有效装饰器签名中的参数是什么意思吗?
declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void;
declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
declare type ParameterDecorator = (target: Function, propertyKey: string | symbol, parameterIndex: number) => void;
Run Code Online (Sandbox Code Playgroud)
另外,在实现装饰器时是否应该记住哪些最佳实践注意事项?
我想为我的 NestJs 应用程序创建一个计划任务。它应该每 X 秒执行一次,因此我使用此处描述的间隔。
该应用程序使用配置文件,因此我可以使用保持间隔可配置。但是我如何将变量传递给 Typescript 装饰器呢?
NestJs为计划任务提供示例存储库
所以根据样本我想要类似的东西
@Injectable()
export class TasksService {
constructor(
private readonly myConfigService: MyConfigService,
) {}
@Interval(this.myConfigService.intervalInMilliseconds)
handleInterval() {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
我必须按照SchedulerRegistry文档中的描述使用吗?看来这对于标准 Typescript 来说是不可能的,请参阅此线程。