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

159
Views
import child class inside parent class in javascript

I have a class hierarchy, like Parent, Child1 that extends the Parent, Child2 etc. Parent stores an array of child items and I need to import Child1 and Child2 inside Parent.

That obviously leads to circular dependency and throws while trying to create either Child1 or Child 2 with the below error:

TypeError: Super expression must either be null or a function

What are the options to resolve that?

For example, consider the following structure:

parent.js

export default class Parent {
    itemArray = []
}

child1.js

import Parent from "./parent.js"
export default class Child1 extends Parent {
}

child2.js

import Parent from "./parent.js"
export default class Child2 extends Parent {
}

itemArray recursively stores either Child1 or Child2

Now I need to type annotate the itemArray so the parent.js becomes like below:

parent.js

import Child1 from "./child1"
import Child2 from "./child2"

export default class Parent {
    @Types(() => [Child1, Child2])
    itemArray = []
}

This annotation allows to assign correct class to each item in the array when the final object is constructed from a plain object. But that's the details. The question is:

How to allow such a structure so it doesn't brake the entire hierarchy?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The solution is simple and clear to understand. The idea behind the solution is importing classes hierarchical in a different module.

// internal.js (New Js File)
// This module imports and exports
// Parent and Child classes will import classes from here.

export { Parent } from "./parent.js";
export { Child1 } from "./child1.js";
export { Child2 } from "./child2.js";

And you can import required class from 'internal.js'. As an example:

// parent.js

import { Child1 } from "./internal.js";
import { Child2 } from "./internal.js";

export class Parent {
     // @Types annotation method here..
     itemArray = [];
}

Output:

// index.js file to output result

import { Parent } from "./internal.js";
import { Child1 } from "./internal.js";
import { Child2 } from "./internal.js";

var child1 = new Child1();
var child2 = new Child2();
var parent = new Parent();

parent.itemArray.push(child1);
parent.itemArray.push(child2);


console.log(parent.itemArray.length); //it prints '2'
about 4 years ago · Juan Pablo Isaza Report
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!