Recursive References in TypeScript

I'm stuck with a very specific TS/JS problem, which may not be solvable but I thought I'd see if anyone here has a cool idea on how to solve it. So I have a file index.ts with a mapping from some keys to objects:
import A from "a.ts";
import B from "b.ts";
...

const mapping = {
"a": A,
"b": B,
...
};
import A from "a.ts";
import B from "b.ts";
...

const mapping = {
"a": A,
"b": B,
...
};
Then, in the files that contain the objects, they may refer to each other, e.g. in a.ts I may have:
import B from "b.ts";

const A = {
"something": B;
...
}

export default A;
import B from "b.ts";

const A = {
"something": B;
...
}

export default A;
Currently with this setup, I get __WEBPACK_DEFAULT_EXPORT__ and Cannot access 'B' before initialization errors.
1 Reply
ed.upton
ed.uptonOP2y ago
I think the main problem here is that I have circular references across files, where A requires B where B may require A It seems as though as long as there's not a circular import to the root, e.g. A -> index -> B -> A its fine
Want results from more Discord servers?
Add your server