Constructor overload in TypeScript

前端 未结 16 2054
滥情空心
滥情空心 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:38

    Your Box class is attempting to define multiple constructor implementations.

    Only the last constructor overload signature is used as the class constructor implementation.

    In the below example, note the constructor implementation is defined such that it does not contradict either of the preceding overload signatures.

    interface IBox = {
        x: number;
        y: number;
        width: number;
        height: number;
    }
    
    class Box {
        public x: number;
        public y: number;
        public width: number;
        public height: number;
    
        constructor() /* Overload Signature */
        constructor(obj: IBox) /* Overload Signature */
        constructor(obj?: IBox) /* Implementation Constructor */ {
            if (obj) {
                this.x = obj.x;
                this.y = obj.y;
                this.width = obj.width;
                this.height = obj.height;
            } else {
                this.x = 0;
                this.y = 0;
                this.width = 0;
                this.height = 0
            }
        }
    
        get frame(): string {
            console.log(this.x, this.y, this.width, this.height);
        }
    }
    
    new Box().frame; // 0 0 0 0
    new Box({ x:10, y:10, width: 70, height: 120 }).frame; // 10 10 70 120
    
    
    
    // You could also write the Box class like so;
    class Box {
        public x: number = 0;
        public y: number = 0;
        public width: number = 0;
        public height: number = 0;
    
        constructor() /* Overload Signature */
        constructor(obj: IBox) /* Overload Signature */
        constructor(obj?: IBox) /* Implementation Constructor */ {
            if (obj) {
                this.x = obj.x;
                this.y = obj.y;
                this.width = obj.width;
                this.height = obj.height;
            }
        }
    
        get frame(): string { ... }
    }
    

提交回复
热议问题