Constructor overload in TypeScript

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

    I know this is an old question, but new in 1.4 is union types; use these for all function overloads (including constructors). Example:

    class foo {
        private _name: any;
        constructor(name: string | number) {
            this._name = name;
        }
    }
    var f1 = new foo("bar");
    var f2 = new foo(1);
    

提交回复
热议问题