typescript TS1241:当作为表达式调用时,无法解析方法装饰程序的签名
我的测试代码如下所示:typescript TS1241:当作为表达式调用时,无法解析方法装饰程序的签名,typescript,decorator,Typescript,Decorator,我的测试代码如下所示: function test(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) { return descriptor; } class Test { @test hello() { } } 我已经说明: --experimentalDecorators--emitDecoratorMetadata似乎TypeScrip
function test(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) {
return descriptor;
}
class Test {
@test
hello() {
}
}
我已经说明:
--experimentalDecorators--emitDecoratorMetadata似乎TypeScript期望decorator函数的返回类型为“any”或“void”。因此,在下面的示例中,如果我们在末尾添加
:any
,它最终会工作
function test(target: Object,
propertyKey: string,
descriptor: TypedPropertyDescriptor<any>): any {
return descriptor;
}
功能测试(目标:对象,
propertyKey:字符串,
描述符:TypedPropertyDescriptor):任何{
返回描述符;
}
使用--目标ES5--发射装饰或数据--实验检测器
或使用以下配置:
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "ES5"
}
}
在tsconfig for use decorator中添加此命令
{
"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;
};
随着时间的推移,这个神秘的错误消息似乎有多个根本原因。截至2019年底,我可以收集到以下信息:
- 这个信息充其量只是误导;这个问题与解析签名的能力无关,也与缺少签名无关。相反,它表明装饰程序存在键入问题。例如,以下代码:
(如果您使用 ()=>{} 函数表示法,切换到正则表示法 函数(){}
这对我很有用。你用哪个版本的typescript编译?tsc test.ts--experimentalDecorators--emitDecoratorMetadata@iberbeutsc—版本1.8.10如何将此
TypedPropertyDescriptor
更改为此PropertyDescriptor
和target:Object
更改为target:any
这是不可能的recognized@Jeff你能在哪里解决/解决这个问题呢?我也遇到了同样的问题,将目标设置为es2015
实际上很有帮助。谢谢!其他人:我能通过使用“allowJs”:true
,使这个工作正常(在JS项目中)。我不需要使用“target”:“ES5”
你能解释为什么会发生这种情况吗?不确定,但可能与绑定有关,()=>{}auto binds没有想到解决方案会这么简单…这是唯一对我有效的方法。设置traget ES5/ES5/es2015没有影响
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;
};
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() { }
}
f(): 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