错误:无法调用类型缺少调用签名的表达式 [英] Error: Cannot invoke an expression whose type lacks a call signature

查看:42
本文介绍了错误:无法调用类型缺少调用签名的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于错误:无法调用类型缺少调用签名的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆