【问题标题】:How can i reuse my model type properties?如何重用我的模型类型属性?
【发布时间】:2021-05-10 15:37:15
【问题描述】:

我有一个 api 调用,其响应如下所示:

export interface UserResponse {
    id: string;
    is_admin: boolean;
    last_name: string;
    phone: string;
    ...

    salary: number;
}

之后我使用 datePipe transform 方法将我的日期转换为一些不同的日期格式。但这给出了字符串。

例如

 this.users[0].salary = this.currencyPipe.transform(this.users[i].salary, 'USD', 'symbol', '1.0-0');

现在我的薪水不是数字类型,而是字符串类型。

所以我制作了另一个模型,现在指示另一种类型的用户 - with salary string but not number

export interface User {
    id: string;
    is_admin: boolean;
    last_name: string;
    phone: string;
    ...

    salary: string;
}

但是我里面有很多重复的属性。 id, is_admin etc.

我怎样才能重复使用它们?

比如我会做第三个接口

export interface UserBase {
    id: string;
    is_admin: boolean;
    last_name: string;
    phone: string;
}

我现在如何将 UserBase 中的所有属性插入UserUserResponse

export interface User {
}

【问题讨论】:

标签: angular typescript


【解决方案1】:

这可以通过继承来实现

export interface User extends UserBase {
    salary: string;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-13
    • 2016-08-21
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多