Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

82
Views
Setting field in superclass isn't propagated to subclass

In an effort to reduce boilerplate in my constructors, I have an abstract class implementation that tries to set fields on the implementing class. A simplified version is below. The problem is that in the superclass constructor, the fields appear to be set, but outside of that constructor, they are undefined.

Code:

abstract class BaseClass {
  constructor(args: any) {
    Object.keys(args).forEach((key) => (this[key] = args[key]));
    console.log("constructed", this);
  }
}

class ChildClass extends BaseClass {
  a: number;
  b: string;

  print() {
    console.log(this);
  }
}

const val = new ChildClass({ a: 1, b: "str" });
val.print();

The output of this code is:

constructed ChildClass { a: 1, b: 'str' }
ChildClass { a: undefined, b: undefined }

Is there any way to make it so that BaseClass's constructor sets values that are accessible from ChildClass?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!