打字稿声明合并,如何取消合并?

时间:2019-04-09 17:42:48

标签: typescript

我要合并下一个Typescript声明:

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

1 个答案:

答案 0 :(得分:1)

我无权访问您使用的类型/修饰符/模块,因此,如果以下任何内容产生错误,您可以考虑将问题中的代码编辑为Minimum, Complete and Verifiable example


您可以尝试通过

来区分类型
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> { }

希望有所帮助;祝你好运!