0

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
Mohsin Amjad
  • 1,045
  • 6
  • 14
  • 1
    Ca you reproduce by this https://codesandbox.io/s/pensive-hermann-g3v4k?file=/src/index.ts ? – Daniil Loban Dec 31 '20 at 11:22
  • @Daniil it's working fine in your sandbox. I guess it has something to do with babel and @nrwl/web/babel. as i mentioned it works fine until babel-plugin-transform-typescript-metadata is added in babel config. – Mohsin Amjad Jan 01 '21 at 07:06

1 Answers1

0

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.

Mohsin Amjad
  • 1,045
  • 6
  • 14