TypeError: 未定义的类扩展值不是函数或 null

在尝试创建这些实体时,我得到了以下错误。

TypeError: Class extends value undefined is not a function or null

我假设这与循环依赖有关,但是在使用表继承和一对多关系时如何避免这种情况呢?

它在 BaseComic_1.BaseComic上抱怨下面的 javascript。

let Variant = class Variant extends BaseComic_1.BaseComic {

这是完整的文件。

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const typeorm_1 = require("typeorm");
const Comic_1 = require("./Comic");
const BaseComic_1 = require("./BaseComic");
let Variant = class Variant extends BaseComic_1.BaseComic {
};
__decorate([
typeorm_1.ManyToOne(type => Comic_1.Comic, comic => comic.variants),
__metadata("design:type", Comic_1.Comic)
], Variant.prototype, "comic", void 0);
Variant = __decorate([
typeorm_1.ClassEntityChild()
], Variant);
exports.Variant = Variant;
//# sourceMappingURL=Variant.js.map

import {Entity, Column, PrimaryGeneratedColumn, OneToMany} from "typeorm";
import {Comic} from "./Comic";


@Entity()
export class Series {


@PrimaryGeneratedColumn()
public id: number;


@Column("text", {
length: 30
})
public copyright: string;


@Column("text", {
length: 100
})
public attributionText: string;


@Column("text", {
length: 150
})
public attributionHTML: string;


@Column("text", {
length: 50
})
public etag: string;


@Column("text", {
length: 200
})
public title: string;


@Column("text")
public description: string;


@Column("number", {
length: 4
})
public startYear: number;


@Column("number", {
length: 4
})
public endYear: number;


@Column("text", {
length: 20
})
public rating: string;


@Column("text", {
length: 20
})
public type: string;


@Column("text")
public thumbnail: string;


@OneToMany(type => Comic, comic => comic.series)
public comics: Array<Comic>;
}

import {Entity, TableInheritance, PrimaryGeneratedColumn, Column, ManyToOne, DiscriminatorColumn} from "typeorm";
import {Series} from "./Series";


@Entity()
@TableInheritance("class-table")
@DiscriminatorColumn({ name: "type", type: "string"})
export class BaseComic {


@PrimaryGeneratedColumn()
public id: number;


@Column("text", {
length: 30
})
public copyright: string;


@Column("text", {
length: 100
})
public attributionText: string;


@Column("text", {
length: 150
})
public attributionHTML: string;


@Column("text", {
length: 50
})
public etag: string;


@Column("text", {
length: 200
})
public title: string;


@Column("int")
public issue: number;


@Column("text")
public variantDescription: string;


@Column("boolean")
public variant: boolean;


@Column("text")
public description: string;


@Column("int")
public pageCount: number;


@Column("date")
public onSaleDate: Date;


@Column("date")
public unlimitedDate: Date;


@Column("text")
public thumbnail: string;


@ManyToOne(type => Series, series => series.comics)
public series: Series;
}

import {OneToMany, ClassEntityChild} from "typeorm";
import {Variant} from "./Variant";
import {BaseComic} from "./BaseComic";


@ClassEntityChild()
export class Comic extends BaseComic {


@OneToMany(type => Variant, variant => variant.comic)
public variants: Variant[];
}

import {ManyToOne, ClassEntityChild} from "typeorm";
import {Comic} from "./Comic";
import {BaseComic} from "./BaseComic";


@ClassEntityChild()
export class Variant extends BaseComic {


@ManyToOne(type => Comic, comic => comic.variants)
public comic: Comic;
}
227503 次浏览

我也有同样的问题。结果是我循环地导入类,这显然是一个限制。(参见这些 GitHub 问题: # 20361# 4149# 10712)

请注意,循环引用似乎也限制在文件之间,而不是简单的类型之间。

这个其他答案

正如 Thomas Jensen 在上面的注释中指出的,循环引用不仅可以出现在 Type 中,也可以出现在文件中。 我在从同一个文件导出基类型和派生类型时遇到了同样的问题。例如:

// index.ts
export { BaseClass } from "./base";
export { DerivedClass } from "./derived";

这是一个很容易掉进去的陷阱。在这里发布这篇文章,希望可以节省其他人的调试时间。

只是碰到这个问题,嗯,这是奇怪的。我运行的项目作为

node --require ts-node/register path/to/index.ts

即使在我按照已接受的答案删除了循环引用之后,这个错误仍然失败了。

但是,如果我运行 tsc,它编译良好,然后它运行良好,甚至与 --require ts-node/register...

希望这对谁有帮助。

我来这里是因为当用 jest 执行代码时,它抛出了这个错误。 这是因为在为 jest.config.js编写 moduleNameMapper时,对象中元素的顺序是必不可少的。

有一个助手从 ts-config.json导入模块名称:

// jest.config.js
const { pathsToModuleNameMapper } = require('ts-jest/utils');
// In the following statement, replace `./tsconfig` with the path to your `tsconfig` file
// which contains the path mapping (ie the `compilerOptions.paths` option):
const { compilerOptions } = require('./tsconfig');


module.exports = {
// [...]
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths /*, { prefix: '<rootDir>/' } */ )
};

拍自 开玩笑的官方文件

我也有同样的问题,因为我的编辑器从错误的包中自动导入了 Entity

一旦我将 import { Entity } from 'typeorm/decorator/entity/Entity';改回 import { Entity } from 'typeorm';,错误消息就消失了。

循环依赖性很难识别。MichaelWeststrate 有一个关于循环依赖的 有趣的读物,并提出了一个修复它们的模式。

自动循环依赖性检测。

除了使用支持可伸缩性的模式之外,您还可以使用一个非常有用的工具 Madge,这个工具可以很轻松地为您识别循环依赖关系。

Madge 可以运行在 .ts.js文件。我发现在这两个目录中运行它是有用的,因为由于翻译过程,它们可能会给出不同的结果。

对于 typecript. ts 文件 :

madge --circular --extensions ts <directory_path>

对于 Javascript. js 文件 :

madge --circular <directory_path>

内部模块模式

试用这个例子来解决循环依赖问题。 你可以在这里找到详细信息

// -- app.js --
import { AbstractNode } from './internal'


/* as is */


// -- internal.js --
export * from './AbstractNode'
export * from './Node'
export * from './Leaf'


// -- AbstractNode.js --
import { Node, Leaf } from './internal'


export class AbstractNode {
/* as is */
}


// -- Node.js --
import { AbstractNode } from './internal'


export class Node extends AbstractNode {
/* as is */
}


// -- Leaf.js --
import { AbstractNode } from './internal'


export class Leaf extends AbstractNode {
/* as is */
}

1. 设计反应元件:

class Card extends React.Compoonent {

请参见输入错误: Compnt

2. 在 Windows 上安装“角度”时出现错误:

TypeError: 未定义的类扩展值不是函数或 null

  1. 开始 > 运行 > AppWiz.cpl > 卸载 node.js

  2. 删除节点安装目录,删除剩余的文件夹和文件

  3. 从开始 > 运行 >% AppData% 删除 npm 文件夹(AppData 漫游)

  4. 如果存在,则从 c: users [ username }目录中删除 npm-cache

  5. 安装 node.js 的最新版本,请使用默认的 C: Program Files nodejs安装路径。

  6. 打开 Cmd:


C:\Users\Jeremy>node -v
v17.1.0


C:\Users\Jeremy>npm -v
8.1.2

在我的节点类型脚本项目中,我使用的是导入/导出语法(超过要求)。

我在一个文件中出现了这个错误,忘记去掉 module.export 语法:

module.exports = { AbstractClass } // this will cause issues if you use import/export syntax

我只需要删除它,并简单地使用“导出”关键字

export class AbstractClass { /* stuff */ }

我收到这个错误是因为我将 Node.js 安装到了文件夹 C:\applications中(不是默认的 C:\Program Files\nodejs)

这很奇怪,但是在将 Node.js 重新安装到默认位置后,我可以在新克隆的项目上成功地运行 npm install

对我来说,这不是循环依赖,这是为角度网络应用程序。

我上了一堂抽象课:

export abstract class BaseService {
...
}

我试图扩展它来实现 Angular 服务,

@Injectable({
providedIn: 'root'
})
export class MyExtendedService extends BaseService {

但是,我在实现这个扩展的 app.module.ts中一直得到错误 TypeError: Class extends value undefined is not a function or null

经过大量的尝试和错误,我发现我必须把 @Injectable也添加到基类中。因此,我将基类更新为:

@Injectable()
export abstract class BaseService {
...
}

然后,一切都很顺利。

我在使用带有基类和继承(扩展关键字即. . export class Arrow extends Pointer {})的 ES6时遇到了同样的错误。

这些建议都没有帮助我解决问题,直到我读到关于用 module.export 替换“ export”关键字的内容... ... 我检查了我的基类,发现我忘记在类签名之前包含 export 关键字... ..。

class Pointer {}

变成了

export class Pointer {}

做出那个改变就能消除我的错误。

我也犯了同样的错误。

原因是我的 index.vue单文件组件导入了包含公共逻辑的 index.ts文件:

// index.vue


import Impl from './index'

文件结构是:

/my-component
index.vue
index.ts

据我所知,index.vue导入了它自己。所以,我把 index.ts重命名为 common.ts