首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何省略嵌套数组类型的属性

如何省略嵌套数组类型的属性
EN

Stack Overflow用户
提问于 2022-11-14 06:04:59
回答 1查看 27关注 0票数 1

我已经定义了2种类型:

代码语言:javascript
复制
export type Pants = {
  __typename?: 'Pants';
  id: Scalars['ObjectID'];
  price: Scalars['Int'];
  color: Scalars['String'];
};

export type Outfits = {
  __typename?: 'Outfits';
  id: Scalars['ObjectID'];
  category: Scalars['String'];
  pants: Array<Pants>;
};

现在,在我的代码的其他地方,我想导入Outfits类型,但忽略Pants对象嵌套数组的price

我很难找到合适的方法去做。

我试过:

代码语言:javascript
复制
type Outfit = Omit<Outfits['pants'], 'price'>;

但这似乎行不通。

如果我想导入我的Outfit并省略category,我会这样做:

代码语言:javascript
复制
type Outfit = Omit<Outfits, 'category'>;

如何导入Outfits类型而忽略Pants对象嵌套数组的price

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-14 09:18:15

您可以使用映射类型来映射Outfits属性和条件类型,以检查键是否为pants,然后从其类型中省略price

代码语言:javascript
复制
type Outfit = {
  [P in keyof Outfits]: P extends "pants"
    ? Omit<Outfits[P][number], "price">[]
    : Outfits[P];
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74427501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档