Nick Hodges
Nick Hodges

Reputation: 17138

What is the proper way to overload a constructor in typescript?

Given this TypeScript class:

export class Person {
    
    public constructor (public firstName: string, public lastName: string) {
    }

    public fullName(): string {
        return `${this.firstName} ${this.lastName}`;
    }

    public WriteInfo() {
        console.log(this.fullName());
    }
}

What is the proper way to override the constructor in a descendent class?

I have:

import { Person } from './class';

export  class Employee extends Person {
    public constructor (afirstName: string, alastName: string, public Salary: number) {
      super(afirstName, alastName);   
    }    

    public WriteInfo() {
        let s: string = `${this.fullName()} makes this much: -- $${this.Salary}`;
        console.log(s);
    }
}

but I am not sure that is the correct declaration for the constructor, particularly with regard to the default properties.

Do the firstName and the lastName parameters of Employee have to be declared public as well? What I have works, but I just want to check to see if this is correct/accepted/proper/conventional.

Upvotes: 0

Views: 3620

Answers (2)

Estus Flask
Estus Flask

Reputation: 222865

The example shows the correct way to do that. Child constructor may have different signature but should call super with correct arguments.

Child constructor doesn't need visibility modifiers for same parameters as in parent constructor, this will result in assigning this.foo = foo twice:

class Foo {
    public constructor (public foo: string) {}
}

class Bar extends {
    public constructor (public foo: string) {
      super(foo);
    }
}

And visibility modifier isn't needed to change visibility because this is not supported:

class Foo {
    public constructor (public foo: string) {}
}

class Bar extends {
    // error
    public constructor (protected foo: string) {
      super(foo);
    }
}

Visibility modifier may be needed if child class has different parameter name and needs to create an alias for the property, this will result in Bar instance having equal foo and bar properties:

class Foo {
    public constructor (public foo: string) {}
}

class Bar extends {
    public constructor (public bar: string, public baz) {
      super(bar);
    }
}

Upvotes: 1

kctang
kctang

Reputation: 11202

Q: Do the firstName and the lastName parameters of Employee have to be declared public as well? A: firstName and lastName does not need to be public unless you need it to be.

Basically, your question is about the role of constructor parameter modifiers, or "variable scope":

  • scope of parameters without modifiers in constructors are only visible within the constructor function.
  • scope of parameters with modifiers (private/protected/public) will be "class properties" with the same modifier. See the referenced link below for example.

So, the choice of whether to use a modifier and which modifier really depends on what scope you want the parameter to have.

Some references you should read:

Upvotes: 1

Related Questions