且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在TypeScript的已实现接口中实现构造函数重载?

更新时间:2022-03-31 22:30:14

实现签名不可见.您需要声明 all 调用者应该看到的重载,然后编写实现主体.

The implementation signature is not visible. You need to declare all the overloads callers should see, then write the implementation body.

export interface Item {
    time: number;
}

export class Foo implements Item {
    public time: number;
    public name: string;

    constructor();
    constructor(
        time: number,
        name: string
    );
    constructor(
        time?: number,
        name?: string
    ) { 
        this.time = id || -1
        this.name = name || ""
      };
}

您还可以阅读 查看全文