I am facing an issue with my class hierarchy, which includes classes like Parent
, Child1
(which extends Parent), and Child2
.
Parent
contains an array of child items, but importing Child1
and Child2
into Parent
leads to circular dependencies and errors when trying to create instances of Child1 or Child2.
The error message encountered is:
TypeError: Super expression must either be null or a function
What are the possible solutions to resolve this issue?
For better understanding, 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 {
}
The itemArray
stores instances of either Child1
or Child2
recursively.
Now, I need to annotate the itemArray
in parent.js
as follows:
parent.js
import Child1 from "./child1"
import Child2 from "./child2"
export default class Parent {
@Types(() => [Child1, Child2])
itemArray = []
}
This annotation ensures that the correct class is assigned to each item in the array during object construction. However, the main question remains:
How can I structure my code to avoid breaking the entire hierarchy?