Javascript/Typescript 中类的交叉引用如何工作?

Posted

技术标签:

【中文标题】Javascript/Typescript 中类的交叉引用如何工作?【英文标题】:How does cross referencing of classes in Javascript/Typescript work? 【发布时间】:2021-04-07 17:03:25 【问题描述】:

我在交叉引用类时遇到问题,即使它们是在一个文件中定义的。

// 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[];
  


错误是这样的

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)

这里是 babel 和 tsconfig 文件

// 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,
    ...
    ...


令人困惑的是,它可以根据 babel 配置正常工作 我正在尝试在我的代码库中添加 inversify,因为我必须在 babel 配置中添加 babel-plugin-transform-typescript-metadata。 这样做之后,我在每个带有交叉引用的类上都遇到了错误,这些错误在添加 babel 插件之前工作正常,所以

是什么导致了这些问题? 如何根据 babel 配置正常工作? 我可以做些什么来修复它,因为它甚至不在可能导致循环依赖的单独文件中

【问题讨论】:

你通过这个codesandbox.io/s/pensive-hermann-g3v4k?file=/src/index.ts复制? @Daniil 它在您的沙箱中运行良好。我想这与 babel 和 @nrwl/web/babel 有关。正如我提到的,它工作正常,直到 babel-plugin-transform-typescript-metadata 被添加到 babel 配置中。 【参考方案1】:

显然这是与“babel-plugin-transform-typescript-metadata”相关的问题,在用“babel-plugin-parameter-decorator”替换该插件后,与循环依赖相关的问题自动解决了。

【讨论】:

以上是关于Javascript/Typescript 中类的交叉引用如何工作?的主要内容,如果未能解决你的问题,请参考以下文章

OC基础--OC中类的定义

Python中类的定制

TypeScript中类的使用

C++ 中类的构造函数理解

java中类的组合机制

java中类的理解