Typescript中不允许多个构造函数的实现?

时间:2019-03-11 13:32:08

标签: angular typescript constructor constructor-overloading

您好,我不知道在Typescript中是否可行,但是不可能重载我的Period类的构造函数

  

不允许使用多个构造函数。ts(2392)

我想做什么:

export class Period {
    id: number;
    year: string;
    month: string;
    blocked: boolean;

    constructor(id: number, year: string, month: string, blocked: boolean) {
        this.id = id;
        this.year = year;
        this.month = month;
        this.blocked = blocked;
    }

    constructor(year: string, month: string, blocked: boolean) {
        this.year = year;
        this.month = month;
        this.blocked = blocked;
    }
}

我希望我可以在插入期间实例化不带ID的Period,例如,对于更新,Period已经具有ID。

我试图在数据库中插入一个null ID的Period,但这会在Postman上返回错误。

enter image description here

另一方面,如果没有ID参数,它将起作用。

enter image description here

如果有人有解决方案,我很感兴趣。 预先谢谢你。

0 个答案:

没有答案