打字稿中的通用类型定义

时间:2019-07-25 04:11:48

标签: angular typescript generics

我有2个api调用,它们在我的角度项目中以以下格式返回我的数据。

  • 在第一个api中,我根据response在下面的类中获得了IMap作为数组
  • 在第2 ap中,我在下面的类中将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可以代表TArray

基本上,它对ObjectGeneric<IMap>都适用,其中Generic<IBenefit>可以是数组或对象

0 个答案:

没有答案