【问题标题】:how to make a type which only extracts property values from the interface in typescript?如何制作一个只从打字稿接口中提取属性值的类型?
【发布时间】:2023-01-14 20:49:54
【问题描述】:

我完全不知道如何用一句话表达我的意图,所以我会尽力而为。

interface A {
  person: {
    name: string;
    age: number;
  },
  count: number
}

type B = Pick<A, 'person'>

// type B = {person: {name: string; age: number;}}

如我们所见,如果我们使用Pick实用程序类型,则类型 B 有一个键person。

如果我想摆脱 key(person),而只想拥有像下面类型 C 那样的属性值怎么办?

 // type C which I want to make.
 type C = {
   name: string;
   age: number;
 }

 // something like StripKeyOut<Pick<A, 'person'>> is possible?

提前感谢您的帮助。

【问题讨论】:

    标签: typescript


    【解决方案1】:

    打字稿有Indexed Access Types:type B = A['person']

    Playground

    【讨论】:

      猜你喜欢
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2021-12-03
      • 1970-01-01
      • 2021-12-04
      • 2019-06-30
      • 1970-01-01
      相关资源
      最近更新 更多