Typescript 实体循环引用得到 Class extends value undefined is not a constructor or null

Typescript entity circular reference got Class extends value undefined is not a constructor or null

所以背景故事是我在一家公司工作并继续他们的代码。我能够在新的 nestjs 项目中重现问题,所以我有 4 个实体(仅用于演示目的)

// User Entity
import { Column, Entity, PrimaryGeneratedColumn, TableInheritance } from "typeorm";
import { PolymorphicChildren } from "typeorm-polymorphic";
import { AdvertEntity } from "./advert.entity";

@Entity('m_users')
@TableInheritance({ column: { type: "varchar", name: "type" } })
export class UserEntity{
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column()
    name: string;

    @Column()
    email: string;

    @Column()
    password: string;

    @PolymorphicChildren(()=>AdvertEntity,{
        eager:false
    })
    adverts: AdvertEntity[];
}
// Employee Entity
import { ChildEntity, Column, JoinTable, ManyToMany } from "typeorm";
import { MerchantEntity } from "./merchant.entity";
import { UserEntity } from "./user.entity";

@ChildEntity()
export class EmployeeEntity extends UserEntity{
    @Column()
    room: string;

    @ManyToMany(()=>MerchantEntity)
    @JoinTable()
    merchants: MerchantEntity[];
}
// Merchant Entity
import { Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from "typeorm";
import { PolymorphicChildren } from "typeorm-polymorphic";
import { AdvertEntity } from "./advert.entity";
import { EmployeeEntity } from "./employee.entity";

@Entity('m_merchants')
export class MerchantEntity{
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column()
    name: string;

    @PolymorphicChildren(()=>AdvertEntity,{
        eager: false
    })
    adverts: AdvertEntity[];

    @ManyToMany(()=>EmployeeEntity)
    @JoinTable()
    employees: EmployeeEntity[];
}
// Adverts Entity
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
import { PolymorphicParent } from "typeorm-polymorphic";
import { PolymorphicChildInterface } from "typeorm-polymorphic/dist/polymorphic.interface";
import { MerchantEntity } from "./merchant.entity";
import { UserEntity } from "./user.entity";

@Entity('m_adverts')
export class AdvertEntity implements PolymorphicChildInterface{

    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column()
    target: string;

    @PolymorphicParent(()=> [ UserEntity, MerchantEntity])
    owner: UserEntity | MerchantEntity;

    @Column()
    entityId: string;

    @Column()
    entityType: string;

}

当我 运行 这些代码时我得到了这个错误

export class EmployeeEntity extends UserEntity{
                                    ^
TypeError: Class extends value undefined is not a constructor or null
    at Object.<anonymous> (/Users/digitalenvision/sandbox/nest-sb-1/src/entity/employee.entity.ts:6:37)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/digitalenvision/sandbox/nest-sb-1/src/entity/merchant.entity.ts:4:1)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
digitalenvision@Digitals-MacBook-Pro-3 nest-sb-1 % npm run start

所以问题是 Merchant、Employee 和 Adverts 之间存在循环引用。当我尝试从 AdvertsEntity 的多态关系中删除 Merchant 或删除 Employee 和 Merchant 之间的多对多关系时,这些问题就消失了。但是我们仍然需要多对多关系和多态关系。我从几个论坛尝试了一些解决方案,但对我来说仍然没有运气。关于这些问题有什么建议吗?谢谢

所以我认为parent不可能有多态关系,我的上司也这么认为。解决方案是与 child 建立多态关系。改变

// User entity
...
// remove these relation
// @PolymorphicChildren(()=>AdvertEntity,{
//        eager:false
//    })
//    adverts: AdvertEntity[];
...
// Employee Entity
...
// add the relation
@PolymorphicChildren(()=>AdvertEntity,{
        eager: false
    })
    adverts: AdvertEntity[];
...
// AdvertEntity
...
// change to 
 @PolymorphicParent(()=> [ EmployeeEntity, MerchantEntity])
    owner: EmployeeEntity | MerchantEntity;
...

解决了我的问题。谢谢