【发布时间】:2017-02-03 03:37:22
【问题描述】:
我是打字稿的新手,我有两门课。在我的父类中:
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 返回的函数确实有调用签名?我想我误解了关于函数类型的重要内容,但我不知道它是什么。
谢谢!
【问题讨论】:
-
togglrBody不应该是一个字符串,因为你希望它是一个函数 -
@eavidan 是的,它是一个实际返回布尔值的函数。我原本以为它会返回一个字符串。那我该怎么改呢?
-
无论 setProp 返回什么,看起来像
<T>(val: T) => T
标签: javascript angularjs typescript types