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.
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.
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!
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[]
.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With