AConsumer
AConsumer

Reputation: 2791

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

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:-

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

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 ?

Upvotes: 12

Views: 72189

Answers (3)

AhammadaliPK
AhammadaliPK

Reputation: 3548

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;

Upvotes: 1

kabanus
kabanus

Reputation: 25980

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.

Upvotes: 4

kctang
kctang

Reputation: 11202

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

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

Similar pattern for all other issues.

Upvotes: 12

Related Questions