Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot invoke an expression whose type lacks a call signature

I have apple and pears - both have an isDecayed attribute:

interface Apple {     color: string;     isDecayed: boolean; }  interface Pear {     weight: number;     isDecayed: boolean; } 

And both types can be in my fruit basket (multiple times):

interface FruitBasket {    apples: Apple[];    pears: Pear[]; } 

Let's assume for now my basket is empty:

const fruitBasket: FruitBasket = { apples: [], pears: [] }; 

Now we take randomly one kind out of the basket:

const key: keyof FruitBasket = Math.random() > 0.5 ? 'apples': 'pears';  const fruits = fruitBasket[key]; 

And of course nobody likes decayed fruits so we pick only the fresh ones:

const freshFruits = fruits.filter((fruit) => !fruit.isDecayed); 

Unfortunately Typescript tells me:

Cannot invoke an expression whose type lacks a call signature. Type '((callbackfn: (value: Apple, index: number, array: Apple[]) => any, thisArg?: any) => Apple[]) | ...' has no compatible call signatures.

What's wrong here - is it just that Typescript doesn't like fresh fruits or is this a Typescript bug?

You can try it yourself in the official Typescript Repl.

like image 735
Erdem Avatar asked Feb 23 '17 22:02

Erdem


People also ask

Has no call signatures TypeScript?

Type 'Number' has no call signatures" TypeScript error occurs when we try to call a number as a function or have typed a function as a number . To solve the error, make sure you're calling a function and it is typed as a function.

What is a call signature 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!


2 Answers

TypeScript supports structural typing (also called duck typing), meaning that types are compatible when they share the same members. Your problem is that Apple and Pear don't share all their members, which means that they are not compatible. They are however compatible to another type that has only the isDecayed: boolean member. Because of structural typing, you don' need to inherit Apple and Pear from such an interface.

There are different ways to assign such a compatible type:

Assign type during variable declaration

This statement is implicitly typed to Apple[] | Pear[]:

const fruits = fruitBasket[key]; 

You can simply use a compatible type explicitly in in your variable declaration:

const fruits: { isDecayed: boolean }[] = fruitBasket[key]; 

For additional reusability, you can also define the type first and then use it in your declaration (note that the Apple and Pear interfaces don't need to be changed):

type Fruit = { isDecayed: boolean }; const fruits: Fruit[] = fruitBasket[key]; 

Cast to compatible type for the operation

The problem with the given solution is that it changes the type of the fruits variable. This might not be what you want. To avoid this, you can narrow the array down to a compatible type before the operation and then set the type back to the same type as fruits:

const fruits: fruitBasket[key]; const freshFruits = (fruits as { isDecayed: boolean }[]).filter(fruit => !fruit.isDecayed) as typeof fruits; 

Or with the reusable Fruit type:

type Fruit = { isDecayed: boolean }; const fruits: fruitBasket[key]; const freshFruits = (fruits as Fruit[]).filter(fruit => !fruit.isDecayed) as typeof fruits; 

The advantage of this solution is that both, fruits and freshFruits will be of type Apple[] | Pear[].

like image 134
Sefe Avatar answered Oct 20 '22 12:10

Sefe


As mentioned in the github issue originally linked by @peter in the comments:

const freshFruits = (fruits as (Apple | Pear)[]).filter((fruit: (Apple | Pear)) => !fruit.isDecayed); 
like image 31
shusson Avatar answered Oct 20 '22 14:10

shusson