我是打字稿的新手,我有两门课。在父类中,我有:
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 都给我错误,“无法调用类型缺少调用签名的表达式。”
但是我认为 setProp 返回的函数确实有调用签名?我想我误解了关于函数类型的重要内容,但我不知道它是什么。
谢谢!
原文由 Justin 发布,翻译遵循 CC BY-SA 4.0 许可协议
它返回的函数有一个调用签名,但是你告诉 Typescript 通过在其签名中添加
: any
来完全忽略它。