typescript 打字稿:无法调用类型缺少调用签名的表达式。“号码”类型没有兼容的呼叫签名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49773231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:53:43  来源:igfitidea点击:

Typescript:Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures

typescript

提问by AConsumer

I know these type of questions are asked before but still it is not clear to me.

我知道以前有人问过这些类型的问题,但我仍然不清楚。

I am a beginner and i was trying to learn import and export in typescript. So i have written the following code. Kindly have a look in to this.

我是初学者,我正在尝试学习打字稿中的导入和导出。所以我写了下面的代码。请看看这个。

I have three files:-

我有三个文件:-

 1.animal.ts  
 2.bird.ts 
 3.application.ts

Animal.ts:-

动物.ts:-

export class Animal {
    name:string
    age:number
    constructor(name:string,age:number){
        this.name=name;
        this.age=age
    }
    
    sleep(){
        console.log("I do sleep")
    }

    eat(){
        console.log("I do eat")
    }
}

bird.ts

鸟.ts

import {Animal} from "./animal"

export class Bird extends Animal{

    constructor(name:string,age:number){
        super(name,age)
    }

    fly(){
        console.log("I can fly also")
    }
}

aplication.ts

应用程序

import { Animal } from "./animal"
import { Bird } from "./bird"

class Application {

    constructor() {
        console.log("Hi i am a bird !!")
    }
}

var animal = new Animal("Rhino", 10);
var bird = new Bird("Pigeon", 3)

animal.age();   //Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures
animal.name();  //Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.

bird.age();     //Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures
bird.fly();
bird.sleep();

How do i solve this problem ? and what this error actually means ?

我如何解决这个问题?这个错误实际上意味着什么?

回答by kctang

bird.age()does not work because it is a number. console.log(bird.age)will work.

bird.age()不起作用,因为它是一个数字。console.log(bird.age)将工作。

bird.fly()works because it is a function.

bird.fly()有效,因为它是一个函数。

Similar pattern for all other issues.

所有其他问题的类似模式。

回答by kabanus

Your problem is not with imports or exports, that looks OK. This:

您的问题不在于导入或导出,看起来没问题。这个:

Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures

says it all. You use age(), so you are using age as if it is a function (has a call signature). But age is a number - you can add it, print it, etc., but you can't "call" it. That's why the other lines don't fail - You can call them properly.

都说了。您使用age(),因此您使用 age 就好像它是一个函数(具有调用签名)。但年龄是一个数字——你可以添加、打印它等,但你不能“调用”它。这就是其他线路不会失败的原因 - 您可以正确调用它们。

回答by AhammadaliPK

This error will also happen when you tried to access a property of a class with parenthesis . For example you have created a property in a class like ,

当您尝试访问带有括号的类的属性时,也会发生此错误。例如,您在类中创建了一个属性,例如,

private get printTemplateDetailsUrl(): string {        
      return `${this._baseHref}://getUserPrintTemplateDetailsJson`;
  } 

and you have tried to access that property like this ,

并且您尝试像这样访问该属性,

let url = this.printTemplateDetailsUrl();

so it will give an error as ,

所以它会给出一个错误,

Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures

so if you want to access that property , you don't need trailing parenthesis , you can access like this ,

所以如果你想访问那个属性,你不需要尾括号,你可以这样访问,

let url = this.printTemplateDetailsUrl;