错误:无法调用类型缺少调用签名的表达式 [英] Error: Cannot invoke an expression whose type lacks a call signature
问题描述
我是打字稿的新手,我有两个班级。在父类我有:
I am brand new to typescript, and I have two classes. In the parent class I have:
abstract class Component {
public deps: any = {};
public props: any = {};
public setProp(prop: string): any {
return <T>(val: T): T => {
this.props[prop] = val;
return val;
};
}
}
在子课程中我有:
class Post extends Component {
public toggleBody: string;
constructor() {
this.toggleBody = this.setProp('showFullBody');
}
public showMore(): boolean {
return this.toggleBody(true);
}
public showLess(): boolean {
return this.toggleBody(false);
}
}
showMore和ShowLess都给我错误,不能调用一个类型没有调用签名的表达式。
Both showMore and ShowLess give me the error, "Cannot invoke an expression whose type lacks a call signature."
但是setProp返回DOES的函数有一个调用签名,我想?我想我误解了关于函数类型的重要事项,但我不知道它是什么。
But the function that setProp returns DOES have a call signature, I think? I think I'm misunderstanding something important about typings of functions, but I don't know what it is.
谢谢!
推荐答案
它返回的函数有一个调用签名,但你通过添加:any
告诉Typescript完全忽略它在其签名中。
The function that it returns has a call signature, but you told Typescript to completely ignore that by adding : any
in its signature.
不要这样做。
这篇关于错误:无法调用类型缺少调用签名的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!