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.
This question is related to
javascript
typescript
types
Perhaps create a shared Fruit
interface that provides isDecayed. fruits
is now of type Fruit[]
so the type can be explicit. Like this:
interface Fruit {
isDecayed: boolean;
}
interface Apple extends Fruit {
color: string;
}
interface Pear extends Fruit {
weight: number;
}
interface FruitBasket {
apples: Apple[];
pears: Pear[];
}
const fruitBasket: FruitBasket = { apples: [], pears: [] };
const key: keyof FruitBasket = Math.random() > 0.5 ? 'apples': 'pears';
const fruits: Fruit[] = fruitBasket[key];
const freshFruits = fruits.filter((fruit) => !fruit.isDecayed);
I had the same issue with numeral, a JS library. The fix was to install the typings again with this command:
npm install --save @types/numeral
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);
Source: Stackoverflow.com