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

如何在TypeScript中实现的接口中进行构造函数重载?,typescript,typescript1.8,Typescript,Typescript1.8,我试图重载实现接口的类的构造函数,但出现以下错误: [0]app/foo.ts(12,5):错误TS2394:重载签名与函数实现不兼容。 课程 导出接口项{ 时间:数字; } 导出类Foo实现项{ 公众时间:数字; 公共名称:字符串; 构造函数(); 建造师( 时间:号码,, 名称:string ) { this.time=id | |-1 this.name=name | |“” }; } 我发现了其他类似的问题(),但我遗漏了一些东西,因为它不起作用。typscript版本为1.8.9

我试图重载实现接口的类的构造函数,但出现以下错误:

[0]app/foo.ts(12,5):错误TS2394:重载签名与函数实现不兼容。
课程

导出接口项{
时间:数字;
}
导出类Foo实现项{
公众时间:数字;
公共名称:字符串;
构造函数();
建造师(
时间:号码,,
名称:string
) { 
this.time=id | |-1
this.name=name | |“”
};
}

我发现了其他类似的问题(),但我遗漏了一些东西,因为它不起作用。typscript版本为1.8.9。

实现签名不可见。您需要声明调用方应该看到的所有重载,然后编写实现体

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 || ""
      };
}

您还可以阅读实施签名不可见的

。您需要声明调用方应该看到的所有重载,然后编写实现体

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 || ""
      };
}
你也可以阅读