Error: Cannot invoke an expression whose type lacks a call signature

Justin picture Justin · Sep 25, 2016 · Viewed 199.5k times · Source

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;
    };
  }
}

In the child class I have:

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);
  }
}

Both showMore and ShowLess give me the error, "Cannot invoke an expression whose type lacks a call signature."

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.

Thanks!

Answer

SLaks picture SLaks · Sep 25, 2016

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

Don't do that.