You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从PhysicalEntry类型中排除customData提取首个Record类型?

从PhysicalEntry交叉类型中排除customData并提取第一个Record类型

要排除PhysicalEntry中的customData字段,提取第一个Record对应的类型,有两种简洁的实现方式:


方法一:直接使用Omit工具类型

Omit<Type, Keys>是TypeScript内置工具类型,专门用于生成排除指定属性的新类型。直接传入要排除的'customData'即可:

type BasePhysicalEntry = Omit<PhysicalEntry, 'customData'>;

最终BasePhysicalEntry会包含color、luster、name、quantity、status、type这些字段,每个字段的类型均为第一个Record定义的{ value: string | number; hasCheck: boolean; readOnly: boolean }。


方法二:拆分键提取与类型选取

如果需要更清晰地展示逻辑步骤,可以先用Exclude排除customData键,再用Pick选取剩余键对应的类型:

// 提取除customData外的所有字段名
type BaseKeys = Exclude<keyof PhysicalEntry, 'customData'>;
// 根据字段名选取对应类型
type BasePhysicalEntry = Pick<PhysicalEntry, BaseKeys>;

这种方式拆分了操作流程,适合需要单独复用键集合的场景。


内容的提问来源于stack exchange,提问作者majkl zumberi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 05:46:06