Javascript 类型错误:类扩展值未定义不是函数或为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43176006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: Class extends value undefined is not a function or null
提问by prolink007
I am getting the following error when trying to create these entities.
尝试创建这些实体时出现以下错误。
TypeError: Class extends value undefined is not a function or null
TypeError: Class extends value undefined is not a function or null
I am assuming this has something to do with circular dependencies, but how is that supposed to be avoided when using table inheritance and one to many relationships?
我假设这与循环依赖有关,但是在使用表继承和一对多关系时应该如何避免这种情况?
It is complaining about the following javascript at BaseComic_1.BaseComic.
它在BaseComic_1.BaseComic.
let Variant = class Variant extends BaseComic_1.BaseComic {
let Variant = class Variant extends BaseComic_1.BaseComic {
Here is the complete file.
这是完整的文件。
"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;
}
回答by Joshua King
I was having the same issue. It turns out I was circularly importing classes, which is apparently a limitation. (See these GitHub issues: #20361, #4149, #10712)
我遇到了同样的问题。事实证明我是循环导入类,这显然是一个限制。(请参阅这些 GitHub 问题:#20361、#4149、#10712)
Note that it seems that the circular reference is also limited between files, not simply types.
请注意,似乎循环引用也仅限于文件之间,而不仅仅是类型。
See thisother answer
看到这个另一个答案
回答by ajxs
As noted in Thomas Jensen's comment above, circular references can occur not just in Types, but also in files. I encountered this same problem when I was exporting both the base and derived types from the same file. Such as:
正如上面 Thomas Jensen 的评论中所指出的,循环引用不仅可以出现在类型中,还可以出现在文件中。当我从同一个文件中导出基本类型和派生类型时,我遇到了同样的问题。如:
// index.ts
export { BaseClass } from "./base";
export { DerivedClass } from "./derived";
This is an easy pitfall to fall into. Posting this here in the hopes it'll save someone else the debugging time.
这是一个容易陷入的陷阱。在这里发布它是希望它可以节省其他人的调试时间。
回答by PeS
Just ran into this issue and, well, it is strange. I am running the project as
刚遇到这个问题,嗯,很奇怪。我正在运行该项目
node --require ts-node/register path/to/index.ts
node --require ts-node/register path/to/index.ts
And this failed with above error even after I removed circular references as suggested by accepted answer.
即使我按照接受的答案的建议删除了循环引用,这也因上述错误而失败。
However, if I run tscit compiles fine and then it runs fine even with --require ts-node/register....
但是,如果我运行tsc它可以正常编译,然后即使使用--require ts-node/register....
Hope this helps someone.
希望这可以帮助某人。
回答by InsOp
I came here because when executing the code with jest, it threw this error.
This is because when writing the moduleNameMapperfor the jest.config.jsthe order of the elements in the object is essential.
我来这里是因为在用开玩笑的方式执行代码时,它抛出了这个错误。这是因为在写moduleNameMapperfor时jest.config.js,对象中元素的顺序是必不可少的。
There is a helper which imports the module-names from the ts-config.json:
有一个帮助程序从以下位置导入模块名称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>/' } */ )
};
Taken from the official documentation of ts-jest
回答by Marvin Scharle
I had the same problem because my editor auto-imported Entityfrom the wrong package.
我遇到了同样的问题,因为我的编辑器Entity从错误的包中自动导入。
Once I changed import { Entity } from 'typeorm/decorator/entity/Entity';back to import { Entity } from 'typeorm';the error message disappeared.
一旦我改import { Entity } from 'typeorm/decorator/entity/Entity';回import { Entity } from 'typeorm';错误信息就消失了。

