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.
Fruit
interface with theisDecayed
property and then declare fruits to be of typeFruit[]
? – Gerrit0key
tostring
i.econst key: string = Math.random() > 0.5 ? 'apples': 'pears';
– shussonany
which circumvents the issue. – shusson