简体   繁体   English

如何从对象中提取某种类型的键

[英]How to extract keys of certain type from object

Lets say I have an interface Obj :假设我有一个接口Obj

interface Obj {
  string1: string
  string2: string
  array1: SomeInterface[]
  array2: SomeOtherInterface[]
}

How can one extract the keys of the interface Obj that extends [] as a discriminated union type?如何将扩展[]的接口Obj提取为可区分的联合类型?

Desired output given Obj from above:从上面给定的Obj所需的输出:

// "array1" | "array2"

You need to use mapped types :您需要使用映射类型

type SomeInterface = { tag: 'SomeInterface' }
type SomeOtherInterface = { tag: 'SomeOtherInterface' }

interface Obj {
    string1: string
    string2: string
    array1: SomeInterface[]
    array2: SomeOtherInterface[]
}

type ObtainKeys<Obj, Type> = {
    [Prop in keyof Obj]: Obj[Prop] extends Type ? Prop : never
}[keyof Obj]

type GetArrayKeys = ObtainKeys<Obj, Array<any>> // "array1" | "array2"

type GetStringKeys = ObtainKeys<Obj, string> //  "string1" | "string2"

Playground 操场

ObtainKeys is a generic util, which expects two arguments. ObtainKeys是一个通用的工具,它需要两个参数。 First one is an Object you want to check, second one is a type you want to extract.第一个是您要检查的对象,第二个是您要提取的类型。

This line: [Prop in keyof Obj]: Obj[Prop] extends Type ? Prop : never这一行: [Prop in keyof Obj]: Obj[Prop] extends Type ? Prop : never [Prop in keyof Obj]: Obj[Prop] extends Type ? Prop : never iterates over all Obj keys and checks whether Obj[Prop] extends desired type. [Prop in keyof Obj]: Obj[Prop] extends Type ? Prop : never迭代所有Obj键并检查Obj[Prop]是否扩展了所需的类型。 Please keep in mind, that Obj[Prop] is a value and not a key.请记住, Obj[Prop]是一个值而不是一个键。 So, if Obj[Prop] meets the requirements - return Prop , otherwise - never .因此,如果Obj[Prop]满足要求 - 返回Prop ,否则 - never

I have used [keyof Obj] in order to get all values of our iterated type, because desired Prop is in the value place and union of any type with never - returns you same type without never我已经使用[keyof Obj]来获取我们迭代类型的所有值,因为所需的Prop位于任何类型的value位置和联合中,并且never - 返回相同类型而没有never

I would like to add that this syntax is also possible for the same use case:我想补充一点,这种语法也适用于相同的用例:

type ObtainKeys<T, V> = keyof {
  [K in keyof T as T[K] extends V ? K : never]: any
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 TypeScript 中的对象中提取所有键 - How to Extract All the keys from an Object in TypeScript 如何在不知道键的情况下提取对象每个值的返回类型 - How to extract the return type of each value of an object without knowing the keys 如何将 object 的部分属性键提取为联合类型? - How to extract part of the properties' keys of an object as a union type? 如何从不可变对象类型中提取可变对象类型 - How to extract mutable object type from immutable object type 如何仅从 Http 响应对象中提取某些字段 - How to extract only certain fields from Http response object 如何从 typescript 中的 object 键生成 object 类型? - How to make object type from an object keys in typescript? 如何重命名 Typescript 类型的某些键 - How rename certain keys for a Typescript type Typescript:如何提取 const object 的值类型并将它们用作新类型中的键? - Typescript: How to extract the value types of a const object and use them as keys in a new type? TypeScript:如何从类型中仅提取类型数组的键和值 - TypeScript: How can I extract from a type only the keys and values of type array Typescript - 如何使用动态对象数组中的键动态键入对象 - Typescript - How to dynamically type an object with keys from a dynamic array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM