gpt4 book ai didi

typescript - TS1238 : Unable to resolve signature of class decorator when called as an expression

转载 作者:搜寻专家 更新时间:2023-10-30 21:07:59 26 4
gpt4 key购买 nike

我看到以下编译错误:

TS1238: Unable to resolve signature of class decorator when called as an expression.

代码如下:

const fdec = function(target:any, field: any, desc: any){
console.log('target 0 :', target);
target.bar = 3;
return target;
};

const fdec2 = function(){
console.log('target 1:');
return function(target:any, field: any, desc: any){
console.log('target 2:', target);
target.bar = 3;
return target;
}
};

@fdec
@fdec2()
class Foo {
static bar: number
}


console.log(Foo.bar);
console.log(new Foo());

有谁知道如何修复该错误?

最佳答案

类装饰器的签名(您可以在 lib.d.ts 中找到)必须如下所示:

declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;

所以你的类装饰器不能有 fielddesc 参数(或者如果你打算将装饰器也用作字段装饰器,它们应该是可选的)

const fdec = function (target: any) {
console.log('target 0 :', target);
target.bar = 3;
return target;
};

const fdec2 = function () {
console.log('target 1:');
return function (target: any) {
console.log('target 2:', target);
target.bar = 3;
return target;
}
};

@fdec
@fdec2()
class Foo {
static bar: number
}


console.log(Foo.bar);
console.log(new Foo());

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com