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

后端 未结 8 2066
执笔经年
执笔经年 2020-12-02 17:50

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 = {         


        
相关标签:
8条回答
  • 2020-12-02 18:18

    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.

    0 讨论(0)
  • 2020-12-02 18:19

    Let's break this down:

    1. The error says

      Cannot invoke an expression whose type lacks a call signature.

    2. The code:

    The problem is in this line public toggleBody: string; &

    it's relation to these lines:

    ...
    return this.toggleBody(true);
    ...
    return this.toggleBody(false);
    
    1. The result:

    Your saying toggleBody is a string but then your treating it like something that has a call signature (i.e. the structure of something that can be called: lambdas, proc, functions, methods, etc. In JS just function tho.). You need to change the declaration to be public toggleBody: (arg: boolean) => boolean;.

    Extra Details:

    "invoke" means your calling or applying a function.

    "an expression" in Javascript is basically something that produces a value, so this.toggleBody() counts as an expression.

    "type" is declared on this line public toggleBody: string

    "lacks a call signature" this is because your trying to call something this.toggleBody() that doesn't have signature(i.e. the structure of something that can be called: lambdas, proc, functions, methods, etc.) that can be called. You said this.toggleBody is something that acts like a string.

    In other words the error is saying

    Cannot call an expression (this.toggleBody) because it's type (:string) lacks a call signature (bc it has a string signature.)

    0 讨论(0)
  • 2020-12-02 18:23

    This error can be caused when you are requesting a value from something and you put parenthesis at the end, as if it is a function call, yet the value is correctly retrieved without ending parenthesis. For example, if what you are accessing is a Property 'get' in Typescript.

    private IMadeAMistakeHere(): void {
        let mynumber = this.SuperCoolNumber();
    }
    
    private IDidItCorrectly(): void {
        let mynumber = this.SuperCoolNumber;
    }
    
    private get SuperCoolNumber(): number {
        let response = 42;
        return response;
    };
    
    0 讨论(0)
  • 2020-12-02 18:26

    "Cannot invoke an expression whose type lacks a call signature."

    In your code :

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

    You have public toggleBody: string;. You cannot call a string as a function. Hence errors on : this.toggleBody(true); and this.toggleBody(false);

    0 讨论(0)
  • 2020-12-02 18:31

    I had the same error message. In my case I had inadvertently mixed the ES6 export default function myFunc syntax with const myFunc = require('./myFunc');.

    Using module.exports = myFunc; instead solved the issue.

    0 讨论(0)
  • 2020-12-02 18:36

    It means you're trying to call something that isn't a function

    const foo = 'string'
    foo() // error
    
    0 讨论(0)
提交回复
热议问题