I have a function filterAssets
which could take 2 different kind of arrays. Below are the 2 different array types:
export interface IResAssetPrice {
currency: string;
price: string;
}
export interface IResAssetSupply {
currency: string;
availableSupply: string;
}
Some filtering takes place then returns the same array. However I get the following error:
Cannot invoke an expression whose type lacks a call signature. Type '{ (callbackfn: (value: IResAssetPrice, index: number, array: IResAssetPrice[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: IResAssetPrice, index: number, array: IResAssetPrice[]) => any, thisArg?: any): IResAssetPrice[]; } | { ...; }' has no compatible call signatures.ts(2349)
export const filterAssets = (assets: IResAssetPrice[] | IResAssetSupply[]): any => {
const filtered = assets.filter((asset) => {
if (asset.availableSupply && asset.availableSupply !== null) {
return asset;
}
if (asset.price && asset.price !== '') {
return asset;
}
});
return filtered;
};
I assumed it had to do with the expected return type, so I also tried the following to no avail.
export const filterAssets = (assets: IResAssetPrice[] | IResAssetSupply[]): {
currency: string;
price: string;
} | {
currency: string;
availableSupply: string;
} => {
const filtered = assets.filter((asset) => {
if (asset.availableSupply && asset.availableSupply !== null) {
return asset;
}
if (asset.price && asset.price !== '') {
return asset;
}
});
return filtered;
};