类 在 Javascript/Typescript 中的交叉引用如何工作?
How does cross referencing of classes in Javascript/Typescript work?
我在交叉引用 class 时遇到问题,即使它们是在一个文件中定义的。
// 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 。
这样做之后,我在每个 class 上得到错误,交叉引用在添加 babel 插件之前工作正常所以
- 是什么导致了这些问题?
- 根据 babel 配置,它如何正常工作?
- 我该怎么做才能修复它,因为它甚至不在可能导致循环依赖的单独文件中
显然这是与“babel-plugin-transform-typescript-metadata”相关的问题,在用“babel-plugin-parameter-decorator”替换该插件后,与循环依赖相关的问题自动解决。
我在交叉引用 class 时遇到问题,即使它们是在一个文件中定义的。
// 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 。 这样做之后,我在每个 class 上得到错误,交叉引用在添加 babel 插件之前工作正常所以
- 是什么导致了这些问题?
- 根据 babel 配置,它如何正常工作?
- 我该怎么做才能修复它,因为它甚至不在可能导致循环依赖的单独文件中
显然这是与“babel-plugin-transform-typescript-metadata”相关的问题,在用“babel-plugin-parameter-decorator”替换该插件后,与循环依赖相关的问题自动解决。