0

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.

2
  • 2
    I am not a TS expert, but this looks good to me... fn and ln are already declared public in the super class no need for this, if it was not good, TS would hit you right into your face ;-) That's why it's there :-) Commented Apr 23, 2018 at 23:39
  • Thnaks @Legends Commented Apr 24, 2018 at 0:53

2 Answers 2

1

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:

Sign up to request clarification or add additional context in comments.

Comments

1

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); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.