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>;
}
解决方案
我无权访问您正在使用的类型/装饰器/模块,因此如果出现以下任何错误,您可能会考虑将问题中的代码编辑为最小、完整和可验证的示例。
您可以尝试通过类似的方式来梳理类型
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> { }
希望有帮助;祝你好运!
推荐阅读
- android - 为什么我的函数在测试连接后总是返回 true
- python - 返回大对象时,Python3 多处理不确定地挂起终止
- javascript - 将道具传递给函数时出现流错误
- angular - 将 Bootstrap 导入 Angular 项目后如何解决 HTML 文件中的波浪线?
- oracle - 在 oracle 10g 中禁用特定时间的表单按钮
- python - 使用 Python 和 MATLAB 时获得不同的逆矩阵
- sql - 触发sql问题
- javascript - 用于构建 URL 的用户输入
- python - 如何在执行 matplotlib 图形后允许 python 代码继续,以便循环可以将实时数据输入 matplotlib 图形
- junit - 如何使用 Mockito 模拟抽象类?