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

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

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

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

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

正在 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;
}

我遇到了同样的问题。原来我是在循环导入 类,这显然是一个限制。 (查看这些 GitHub 个问题:#20361, #4149, #10712

请注意,循环引用似乎也仅限于文件之间,而不仅仅是类型。

参见其他答案

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

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

这是一个容易掉入的陷阱。将其张贴在这里,希望它能为其他人节省调试时间。

只是 运行 进入这个问题,好吧,它是 st运行ge。我 运行 将项目设置为

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>/' } */ )
};

取自official documentation of ts-jest

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

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

循环依赖关系很难识别。 Michael Weststrate 有一个关于循环依赖的 interesting reading 并提出了一个模式来修复它们。

自动循环依赖检测。

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

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

对于 Typescript .ts 文件:

madge --circular --extensions ts <directory_path>

对于 Javascript.js 文件:

madge --circular <directory_path>

内部模块模式

试试这个例子来解决循环依赖。 您可以找到详细信息 Here

// -- 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.设计 React 组件:

class Card extends React.Compoonent {

查看错别字:Compoonent

2。在 Windows 上安装 Angular 失败并出现错误:

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

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

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

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

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

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

  6. 打开命令:


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

C:\Users\Jeremy>npm -v
8.1.2

在我的节点打字稿项目中,我使用了 import/export 语法(超过要求)。

我在忘记删除 module.exports 语法的文件中遇到此错误:

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

这很奇怪,但是在 re-installing Node.js 进入默认位置后,我可以在我新克隆的项目上成功 运行 npm install

对我来说,这不是循环依赖。这适用于 Angular 网络应用程序。

我有一个摘要class:

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 添加到基础 class 中。所以,我将我的基础 class 更新为:

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

然后,一切正常。