My test code is like the following:
function test(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) {
return descriptor;
}
class Test {
@test
hello() {
}
}
but the compiler give me error
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.
I have already specified: --experimentalDecorators --emitDecoratorMetadata
It seems that TypeScript expects the return type of the decorator function to be 'any' or 'void'. So in the example below, if we add : any
to the end, it ends up working.
function test(target: Object,
propertyKey: string,
descriptor: TypedPropertyDescriptor<any>): any {
return descriptor;
}
Use --target ES5 --emitDecoratorMetadata --experimentalDecorators
or use the following config:
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "ES5"
}
}
This cryptic error message seems to have had multiple root causes over time. As of end of 2019, here is what I could gather:
@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() { }
}
{"compilerOptions": { "target": "ES3" } }
results inf(): evaluated main-2.js line 1134 > eval:9:13 g(): evaluated main-2.js line 1134 > eval:15:13 g(): called with 2 arguments main-2.js line 1134 > eval:17:17 f(): called with 2 arguments(đź’ˇ When trying out the code on
typescriptlang.org/play
, first open the JavaScript console in your browser's Developer Tools; then click Run)."target": "ES5"
results inf(): evaluated main-2.js line 1134 > eval:9:13 g(): evaluated main-2.js line 1134 > eval:15:13 g(): called with 3 arguments main-2.js line 1134 > eval:17:17 f(): called with 3 argumentsand accordingly, TypeScript is fully happy with
@f()
(and also @g()
) in that case.The simplest workaround is therefore to pretend that the third parameter is optional, i.e.
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() { }
}
which type-checks successfully under both "target": "ES3"
and "target": "ES5"
.
This “cheat” is especially important if using angular-meteor, in which case you most definitely do not want to mess with the "target"
setting in tsconfig.json
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With