【发布时间】:2019-07-09 23:38:59
【问题描述】:
我有一个函数filterAssets,它可以接受两种不同类型的数组。以下是 2 种不同的数组类型:
export interface IResAssetPrice {
currency: string;
price: string;
}
export interface IResAssetSupply {
currency: string;
availableSupply: string;
}
一些过滤发生然后返回相同的数组。但是我收到以下错误:
无法调用类型缺少调用签名的表达式。类型 '{ (callbackfn: (value: IResAssetPrice, index: number, array: IResAssetPrice[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: IResAssetPrice, index: number, array: IResAssetPrice[]) => any, thisArg?: any): IResAssetPrice[]; } | { ...; }' 没有兼容的调用签名。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;
};
我认为它与预期的返回类型有关,所以我也尝试了以下方法,但无济于事。
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;
};
【问题讨论】:
标签: javascript typescript