41

Why is it not allowed to have separate constructor definitions in TypeScript?
To have e.g. two constructors, I need to write my code like this.

constructor(id: number)
constructor(id: number, name?: string, surname?: string, email?: string) {
    this.id = id;
    this.name = name;
    this.surname = surname;
    this.email = email;
}

Thereby I need to put ? after the parameters that are not required in the first constructor.

Why can't I write it like this?

constructor(id: number) {
    this.id = id;
}

constructor(id: number, name: string, surname: string, email: string) {
    this.id = id;
    this.name = name;
    this.surname = surname;
    this.email = email;
}

So that for both constructors all parameters are mandatory.

Moreover, if I need to have an empty constructor things get even weirder, since I need to mark every parameter with a ?.

constructor()
constructor(id?: number, name?: string, surname?: string, email?: string) {
    this.id = id;
    this.name = name;
    this.surname = surname;
    this.email = email;
}

Why does TypeScript differs from common languages like C# or Python here?

I would expect it to work like this.

constructor() {

}
constructor(id: number, name: string, surname: string, email: string) {
    this.id = id;
    this.name = name;
    this.surname = surname;
    this.email = email;
}

So you can pass none parameter or must pass all parameters.

3
  • 1
    Actually I would not need constructor() at all, if all parameters are marked with a ?. Commented Mar 14, 2016 at 21:41
  • Just so you know the signature ()+(id?) does not expose the id signature. You would need ()+(id)+(id?). See my answer for details Commented Mar 14, 2016 at 23:40
  • Also not every public overload needs to be optional. So your public interface can be safe. Again, see my answer. Commented Mar 14, 2016 at 23:43

4 Answers 4

30

Because your constructor implementation is called by all your overload constructors. (Technically, at runtime there's only one constructor function that gets called with the various overload argument signatures.)

Imagine it like this:

overload_constructor(id:string) {
    implementation_constructor(id);
}

implementation_constructor(id:string, name?:string, age?:number) {
    // ...
}

Thinking of it this way, overload_constructor could not call implementation_constructor unless name and age are optional.

Also see Basarat's answer, the implementation isn't exposed for public usage by the type checker (though at runtime it's the "real" constructor used in JS). If you want to only allow (), (id), or (id, name, surname, email) as the only valid call signatures you would do it like this:

constructor()
constructor(id: number)
constructor(id: number, name: string, surname: string, email: string)
constructor(id?: number, name?: string, surname?: string, email?: string) {
    this.id = id;
    this.name = name;
    this.surname = surname;
    this.email = email;
}

Note that in the implementation all parameters are optional, but that signature is not exposed when compiling and you can only use these these calls:

new Foo()
new Foo(1)
new Foo(1, "a", "b", "c")

Not, for example:

new Foo(1, "a")
Sign up to request clarification or add additional context in comments.

4 Comments

That makes sense. Do you know if this is a limitation to JavaScript (ES5) to which TypeScript get transpilt or it is a design decision of TypeScript developers?
Well, ES5 doesn't have classes at all, only constructor functions. You can't have multiple constructor functions represent a single object type. In ES6 we have classes but only a single constructor function. In JS all function parameters are optional all the time, and untyped. This is why we use TypeScript. :-)
So why is there only one constructor function in TypeScript (at runtime)? I don't get the benefit :D
There's only one constructor function in TypeScript at runtime because it's transpiled to JS for runtime, and JS doesn't support multiple constructors in any way. The constructor overloads are just for the TypeScript compiler for better static typing.
29

The last function overload is only used in the implementation and not available publicly. This is shown below:

class Foo{
    constructor()
    constructor(id?: number) {
    }
}

const foo1 = new Foo();
const foo2 = new Foo(123); // Error! : not public

If you want id:number to be available publically ofcourse you can add another overload:

class Foo{
    constructor()
    constructor(id: number)
    constructor(id?: number) {
    }
}

const foo1 = new Foo();
const foo2 = new Foo(123); // Okay
const foo3 = new Foo('hello'); // Error: Does not match any public overload

The reason is that TypeScript tries not to do fancy code generation for function overloading (traditional languages do this using name mangling e.g. C++)

So you can pass none parameter or must pass parameters.

Actually you can make the final overload optional but none of the public ones as optional. Consider the following example:

class Foo{  
    constructor(id: number, name:string)
    constructor(name:string)
    constructor(idOrName?: number|string, name?:string) {
    }
}

const foo1 = new Foo('name'); // Okay
const foo2 = new Foo(123); // Error: you must provide a name if you use the id overload
const foo3 = new Foo(123,'name'); // Okay

Comments

3

You can use Builder pattern to solve this. Even in C# or Python, it quickly becomes a better approach as the number of constructor arguments grows.

class Foo {
  constructor(public id: number, public name: string, public surname: string, public email: string) {
  }
  static Builder = class {
    id: number = NaN;
    name: string = null;
    surname: string = null;
    email: string = null;
    Builder() {
    }
    build(): Foo {
      return new Foo(this.id, this.name, this.surname, this.email);
    }
  }
}

Comments

-1

If you use static methods to implement overload contructors, see.

export class User implements IUser {
     constructor(
        private _id: string,
        private _name: string,
        private _email: string,
      ) {}
    static New(jsonUser:string){
        return new User(
            JSON.parse(jsonUser).id,
            JSON.parse(jsonUser).name,
            JSON.parse(jsonUser).email)
    }
}

1 Comment

can you then overload the static method?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.