【问题标题】:typescript TS1241: Unable to resolve signature of method decorator when called as an expressiontypescript TS1241:作为表达式调用时无法解析方法装饰器的签名
【发布时间】:2016-10-08 05:44:10
【问题描述】:

我的测试代码如下:

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

【问题讨论】:

  • 它对我有用。你正在编译哪个版本的打字稿?
  • tsc test.ts --experimentalDecorators --emitDecoratorMetadata
  • @iberbeu tsc --version 版本 1.8.10
  • 如何将此TypedPropertyDescriptor&lt;any&gt; 更改为PropertyDescriptor 并将target: Object 更改为target: any?您的声明似乎没有被识别
  • @Jeff 您在哪里可以解决/解决这个问题?我也有同样的问题

标签: typescript decorator


【解决方案1】:

TypeScript 似乎期望装饰器函数的返回类型为“any”或“void”。所以在下面的例子中,如果我们在末尾添加: any,它就可以工作了。

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

【讨论】:

  • 这是唯一对我有用的东西。设置traget ES5/es5/es2015 没有影响
【解决方案2】:

使用--target ES5 --emitDecoratorMetadata --experimentalDecorators

或使用以下配置:

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "ES5"
  }
}

【讨论】:

  • 在我的情况下,将目标设置为 es2015 实际上有帮助。
  • 谢谢!对于其他人:我能够通过使用"allowJs": true 来完成这项工作(在 JS 项目中)。我不需要使用"target": "ES5"
【解决方案3】:

随着时间的推移,这条神秘的错误消息似乎有多个根本原因。截至 2019 年底,我可以收集到以下信息:

  • 该信息充其量是误导性的;该问题与解决签名的能力无关,或者缺乏解决签名的能力。相反,它表示装饰器上的打字问题。比如下面的代码signals TS1241 at @f(), but not at @g()
function f() {
    console.log("f(): evaluated");
    return function (targetClass: any, propertyKey: string, descriptor: TypedPropertyDescriptor<() => void>) {
        console.log("f(): called with " + arguments.length + " arguments");
    }
}

function g() {
    console.log("g(): evaluated");
    return function (target: any, propertyKey: string) {
        console.log("g(): called with " + arguments.length + " arguments");
    }
}

class C {
    @f()      // TypeScript signals TS1241 here
    @g()      // but not there
    method() { }
}
  • 装饰器的调用约定,以及它们的类型,取决于目标 JavaScript 方言。例如,使用 {"compilerOptions": { "target": "ES3" } } 运行上述代码会导致
    f():评估 main-2.js 第 1134 行 > eval:9:13
    g():评估 main-2.js 第 1134 行 > eval:15:13
    g():使用 2 个参数调用 main-2.js 第 1134 行 > eval:17:17
    f():使用 2 个参数调用
    (? 当 trying out the code on typescriptlang.org/play 时,首先在浏览器的开发者工具中打开 JavaScript 控制台;然后单击运行)。
    另一方面,running the very same code under "target": "ES5" 导致
    f():评估 main-2.js 第 1134 行 > eval:9:13
    g():评估 main-2.js 第 1134 行 > eval:15:13
    g():使用 3 个参数调用 main-2.js 第 1134 行 > eval:17:17
    f():使用 3 个参数调用
    
    因此,TypeScript 在这种情况下对@f()(以及@g())非常满意。

因此,最简单的解决方法是假装第三个参数是可选的,

function f() {
    console.log("f(): evaluated");
    return function (targetClass: any, propertyKey: string, descriptor?: TypedPropertyDescriptor<() => void>) {
        console.log("f(): called with " + arguments.length + " arguments");
    }
}

class C {
    @f()
    method() { }
}

"target": "ES3""target": "ES5" 下都成功地进行了类型检查。

如果使用angular-meteor,这种“作弊”尤其重要,在这种情况下,您绝对想弄乱tsconfig.json 中的"target" 设置。

【讨论】:

  • 这个答案救了我两次。
【解决方案4】:

如果你正在使用,你也会得到这个错误

() => {}

函数表示法,切换到正则表示法

函数() {}

【讨论】:

  • 你能解释一下为什么会这样吗?
  • 不确定,但可能与绑定有关,() => {} 自动绑定
【解决方案5】:

在 tsconfig 中添加此命令以使用装饰器。

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "ES5"
  }
}

装饰器功能应该是这样的。

function(target: any, key: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
    descriptor.value = async function(...args: any) {
      try {
        const result = await originalMethod.apply(this, args);
        return result;
      } catch (error) {
         console.log(error)
      }
    };

    return descriptor;
  };

【讨论】:

    猜你喜欢
    • 2018-12-03
    • 1970-01-01
    • 2016-07-26
    • 2019-04-18
    • 2020-01-30
    • 2021-07-06
    • 1970-01-01
    • 2020-09-07
    • 2016-04-02
    相关资源
    最近更新 更多