Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

typescript

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 ?

like image 806
AConsumer Avatar asked Apr 11 '18 10:04

AConsumer


People also ask

Does TS have a no call signature?

Type 'X' has no call signatures" TypeScript error occurs when we try to call a type that isn't a function, or is typed as something else, as a function. To solve the error, make sure you're calling a function and it is typed as a function. Here is an example of how the error occurs.

What is call signature in TypeScript?

What is call signature in TypeScript? In TypeScript we can express its type as: ( a : number , b : number ) => number. This is TypeScript's syntax for a function's type, or call signature (also called a type signature). You'll notice it looks remarkably similar to an arrow function—this is intentional!


1 Answers

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.

like image 149
kctang Avatar answered Nov 17 '22 01:11

kctang