错误:无法调用类型缺少调用签名的表达式

问题描述:

我是打字稿的新手,我有两个班级.在父类中,我有:

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 返回的函数确实有调用签名,我想?我想我误解了一些关于函数类型的重要内容,但我不知道它是什么.

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.

谢谢!

它返回的函数有一个调用签名,但是你告诉 Typescript 通过在它的签名中添加 : any 来完全忽略它.

The function that it returns has a call signature, but you told Typescript to completely ignore that by adding : any in its signature.