I am encountering difficulties with cross-referencing classes that are defined in the same file.
// classes.ts
export class A extends BaseModel implements IA {
static readonly modelName = 'A';
b?: B;
symbol?: string;
constructor(object: IA | A = {} as IA) {
super(object);
const { symbol, b } = object as A;
this.symbol = symbol;
this.b = b;
}
}
export class B extends BaseModel implements IB {
static readonly modelName = 'B';
a?: A[];
constructor(object: IB | B = {} as IB) {
super(object);
const { a } = object as B;
this.a = a as A[];
}
}
The error I receive looks like this:
Uncaught ReferenceError: Cannot access 'B' before initialization
at Module.../../../../libs/platform/src/data/models/common/uom.ts (uom.ts:13)
at __webpack_require__ (bootstrap:84)
at Module.../../../../libs/platform/src/data/models/common/common-models.ts (main.js:27248)
at __webpack_require__ (bootstrap:84)
at Module.../../../../libs/platform/src/data/models/common/index.ts (index.ts:1)
at __webpack_require__ (bootstrap:84)
at Module.../../../../libs/platform/src/data/models/index.ts (index.ts:1)
at __webpack_require__ (bootstrap:84)
at Module.../../../../libs/platform/src/data/index.ts (index.ts:1)
at __webpack_require__ (bootstrap:84)
at Module.../../../../libs/platform/src/index.ts (index.ts:1)
at __webpack_require__ (bootstrap:84)
at Module.../../../../libs/platform-frontend/src/core/client/frontend-client.ts (index.ts:1)
at __webpack_require__ (bootstrap:84)
at Module.../../../../libs/platform-frontend/src/core/client/index.ts (index.ts:1)
at __webpack_require__ (bootstrap:84)
Here are my babel and tsconfig files:
// babel.config.json
{
"presets": [
"@nrwl/web/babel",
"@nrwl/react/babel"
],
"plugins": [
"babel-plugin-transform-typescript-metadata"
],
"babelrcRoots": [
"*"
]
}
//tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"downlevelIteration": true,
"types": ["jest", "node", "cypress","reflect-metadata"],
"resolveJsonModule": true,
"esModuleInterop": true,
"rootDir": ".",
"sourceMap": false,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"importHelpers": false,
"target": "es5",
"allowJs": true,
"typeRoots": ["node_modules/@types"],
"lib": [
"es2017",
"es6",
"dom",
"es2016",
"esnext.asynciterable",
"es5",
"scripthost",
"es2015.promise",
"es2015.collection"
],
"baseUrl": ".",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
...
...
}
I am confused as to why it works fine depending on the babel config. I am attempting to integrate inversify into my codebase, for which I need to include babel-plugin-transform-typescript-metadata in the babel configuration. After adding the plugin, I start getting errors on each class with cross-referencing that were previously working correctly without the plugin. So,
- What is causing these issues?
- How does it work fine based on the babel config?
- What can I do to fix it since the classes are not even in separate files to cause circular dependencies?