TypeScript ClassDecorator目标类型

问题描述 投票:0回答:1

我正在用打字稿写自己的装饰器。目前,我的装饰器看起来像这样:

const Controller = (prefix = ''): ClassDecorator => (target: any): void => {
  // My Logic
};

export default Controller;

我的问题是关于ClassDecorator参数的。当前,我正在为any参数使用target类型,但此参数需要特定的类型。所以我的问题是这种类型如何命名?

我用谷歌搜索了一段时间,但对此一无所获。

typescript decorator typescript-decorator
1个回答
0
投票

您不需要明确声明Controller的返回类型,因为编译器会推断出它。只要您键入内部函数的参数。.

type MyType = { foo: number }

function f() {
  console.log('f(): evaluated')
  return function (target: MyType) {
    console.log('f(): called' + target.foo)
  }
}

``

© www.soinside.com 2019 - 2024. All rights reserved.