小编Ted*_*Ted的帖子

TypeScript中的构造函数重载

有没有人在TypeScript中完成构造函数重载.在语言规范的第64页(v 0.8)中,有一些语句描述构造函数重载,但没有给出任何示例代码.

我现在正在尝试一个非常基本的课堂宣言; 看起来像这样,

interface IBox {    
    x : number;
    y : number;
    height : number;
    width : number;
}

class Box {
    public x: number;
    public y: number;
    public height: number;
    public width: number;

    constructor(obj: IBox) {    
        this.x = obj.x;
        this.y = obj.y;
        this.height = obj.height;
        this.width = obj.width;
    }   

    constructor() {
        this.x = 0;
        this.y = 0;
        this.width = 0;
        this.height = 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

当使用tsc BoxSample.ts运行时,它会抛出一个重复的构造函数定义 - 这很明显.任何帮助表示赞赏.

constructor overloading typescript

344
推荐指数
9
解决办法
20万
查看次数

标签 统计

constructor ×1

overloading ×1

typescript ×1