Search code examples
javascripttypescriptbabeljsnrwlinversifyjs

How does cross referencing of classes in Javascript/Typescript work?


I'm facing problems in cross referencing of classes even if they are defined in one 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[];
  }
}

and the error is something 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 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,
    ...
    ...
}

The confusion is that it works fine depending on babel config I'm trying to add inversify in my code base for that i have to add babel-plugin-transform-typescript-metadata in babel config. After doing that i get errors on each class with cross referencing which were working fine before adding babel plugin so

  • What's causing these issues ?
  • How it's working fine depending on babel config ?
  • What can i do to fix it as it's not even in seperate file which can cause circular dependency

Solution

  • Apparently it was issue related to "babel-plugin-transform-typescript-metadata", after replacing that plugin with "babel-plugin-parameter-decorator" the issue related to circular dependency got resolved automatically.