首页 > 解决方案 > 打字稿声明合并,如何取消合并?

问题描述

我有下一个打字稿声明合并:

import { Collection, Entity, IEntity, OneToMany, PrimaryKey, Property } from "mikro-orm";
import { ObjectId } from "mongodb";
import { LocationModel } from "./locationModel";

@Entity({ collection: "business" })
export class BusinessModel {
    @PrimaryKey()
    public _id!: ObjectId;

    @Property()
    public name!: string;

    @Property()
    public description!: string;

    @OneToMany({ entity: () => LocationModel, fk: "business" })
    public locations: Collection<LocationModel> = new Collection(this);
}

export interface BusinessModel extends IEntity<string> { }

现在,我怎样才能取消合并它们?获取等效于的接口或数据类型:

export interface BusinessEntity {
    _id: ObjectId;
    name: string;
    description: string;
    locations: Collection<LocationModel>; 
}

标签: typescript

解决方案


我无权访问您正在使用的类型/装饰器/模块,因此如果出现以下任何错误,您可能会考虑将问题中的代码编辑为最​​小、完整和可验证的示例


您可以尝试通过类似的方式来梳理类型

type BusinessEntity = 
  Pick<BusinessModel, Exclude<keyof BusinessModel, keyof IEntity<string>>>

但这只有在IEntity的键与您添加的键不重叠时才有效BusinessModel。一个更好的主意是在合并之前捕获您关心的类型:

@Entity({ collection: "business" })
export class BusinessEntity {
  @PrimaryKey()
  public _id!: ObjectId;

  @Property()
  public name!: string;

  @Property()
  public description!: string;

  @OneToMany({ entity: () => LocationModel, fk: "business" })
  public locations: Collection<LocationModel> = new Collection(this);
}

export class BusinessModel extends BusinessEntity { }
export interface BusinessModel extends IEntity<string> { }

希望有帮助;祝你好运!


推荐阅读