相关疑难解决方法(0)

如何实现打字稿装饰器?

TypeScript 1.5现在有装饰器.

有人可以提供一个简单的例子来演示实现装饰器的正确方法,并描述可能的有效装饰器签名中的参数是什么意思吗?

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)

另外,在实现装饰器时是否应该记住哪些最佳实践注意事项?

decorator typescript

198
推荐指数
3
解决办法
7万
查看次数

将变量从配置服务传递到 Typescript 装饰器

我想为我的 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 来说是不可能的,请参阅此线程

javascript node.js typescript nestjs

2
推荐指数
1
解决办法
2877
查看次数

标签 统计

typescript ×2

decorator ×1

javascript ×1

nestjs ×1

node.js ×1