Constructor overload in TypeScript

前端 未结 16 2065
滥情空心
滥情空心 2020-11-28 00:42

Has anybody done constructor overloading in TypeScript. On page 64 of the language specification (v 0.8), there are statements describing constructor overloads, but there wa

16条回答
  •  借酒劲吻你
    2020-11-28 01:40

    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) {
            const { x, y, height, width } = { x: 0, y: 0, height: 0, width: 0, ...obj }
            this.x = x;
            this.y = y;
            this.height = height;
            this.width = width;
        }
    }
    

提交回复
热议问题