I have been struggling with a problem that seems to stem from a circular dependency. Despite conducting thorough research, I have not been able to find a suitable solution. It appears to be similar to the issue discussed here: TypeError: b is undefined in __extends in TypeScript, but the suggested solutions did not work for me.
To simplify the problem, I have created a sample in this plunker.
In essence, there are 3 classes involved:
- Class
A
, which includes an array of typeA
- Class
B
, which inherits from classA
- Class
F
, a factory responsible for creating instances of either classA
or classB
based on a given value
The objective is to manage a dynamic parameter list (each instance of A
representing a parameter) where B
serves as a specialized version of A
for file handling. Even after removing the factory and simplifying to just classes A
and B
, I continue to encounter the same error:
TypeError: b is undefined
Error loading http://localhost:3000/app/main.js
Below is the code snippet from a.ts:
import { F } from './f';
export class A {
children: A[]
constructor(hasChildren: boolean = false) {
if (hasChildren) {
for (var i = 0 ; i < 10 ; ++i) {
let isB = (Math.random() * 2) > 1;
this.children.push(F.createObject(isB))
}
}
}
}
From b.ts:
import { A } from './a';
export class B extends A {
}
And finally, f.ts:
import { A } from './a'
import { B } from './b'
export class F {
static createObject(isB: boolean): A {
if (isB) {
return new B
} else {
return new A
}
}
}