我有2个api调用,它们在我的角度项目中以以下格式返回我的数据。
response
在下面的类中获得了IMap
作为数组response
作为对象
IBenefit
我的通用界面是:
export interface Generic<T> {
responseCode: string;
message: string;
response: T[];
}
IMap
界面如下:
export interface IMap {
countryCode: string;
countryName: string;
numberOfAccounts: number;
totalAmount: number;
surplusAmount: number;
deficitAmount: number;
offsetAmount: number;
noOfCurrencies: number;
accounts: [
{
accountId: string;
accountName: string;
accountLocationCode: string;
accountCity: string;
accountHoldingEntity: string;
accountHoldingEntityLocationCode: string;
accountDescription: string;
accountCurrencyCode: string;
balanceCurrencyCode: string;
averageBalance: number
}
];
}
Ibenefit
界面在下面。
export interface IBenefit {
marketBenefits: object;
currencyBenefits: object;
totalBenefits: object;
yield: object;
}
这适用于第一个API,这对于具有response:T[]
的{{1}}参数很明显,但对于IMap[]
却不是数组,则失败。
如何编写泛型,以便传递的类型IBenfit
可以代表T
和Array
。
基本上,它对Object
和Generic<IMap>
都适用,其中Generic<IBenefit>
可以是数组或对象