typescript TS1241 : Unable to resolve signature of method decorator when called as an expression

标签 typescript decorator

我的测试代码如下:

function test(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) {
    return descriptor;
}

class Test {
    @test
    hello() {
    }
}

但是编译器报错

Error:(33, 5) TS1241: Unable to resolve signature of method decorator when called as an expression.
 Supplied parameters do not match any signature of call target.

我已经指定: --experimentalDecorators --emitDecoratorMetadata

最佳答案

似乎 TypeScript 期望装饰器函数的返回类型为“any”或“void”。因此,在下面的示例中,如果我们将 : any 添加到末尾,它就会正常工作。

function test(target: Object, 
              propertyKey: string, 
              descriptor: TypedPropertyDescriptor<any>): any {
    return descriptor;
}

关于 typescript TS1241 : Unable to resolve signature of method decorator when called as an expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37694322/

相关文章:

javascript - 使用 lodash 根据类型对多个对象进行分组

typescript - lodash 的 _.memoize 与 typescript 方法

python - 我应该如何使用 python 装饰器而不更改函数名称?

python - 在 Python 中使用装饰器将函数变成生成器

typescript - 如何使用 Jest 在 TypeORM 中 stub EntityManager 和 Connection

javascript - 为什么我不能使用 SystemJS 直接从 node_modules 导入?

python - login_required 装饰器给出 "object has no attribute ' user'"错误

typescript - 汇总 typescript 装饰器支持

angular - 显示错误 "Can' t 查找名称日期的 Visual Studio 代码”

node.js - 如何在nestjs中手动注入(inject)依赖