简体   繁体   English

Built-in utility type Pick 说明

[英]Built-in utility type Pick explanation

Can someone explain how built-in Pick<Type, Keys> works?有人可以解释内置的 Pick<Type, Keys> 是如何工作的吗?

We have:我们有:

  1. Implementation of Pick.实施选择。
type Pick<T,K extends keyof T>={  // (1)
    [P in K]:T[P]                 // (2)
}
  1. Interface of Todo object Todo的界面 object
interface Todo {
    title: string
    description: string
    completed: boolean
}
  1. Our object我们的 object
const obj: Pick<Todo,"title">={  // (3)
    title:"Hello",
}

I don't know why we extend K in point #1?我不知道为什么我们要在第 1 点扩展 K?
We have the key "title" in point #3 and then "title" extends keyof Todo in point #1.我们在第 3 点有键“title”,然后“title”在第 1 点extends keyof Todo的键。
Therefore in point #2 we have [P in "title"|"description"|"completed"].因此在第 2 点我们有 [P in "title"|"description"|"completed"]。
In that case we can use [P in keyof T] instead, but it doesn't work of cource.在那种情况下,我们可以使用 [P in keyof T] 来代替,但这当然不起作用。
I need help.我需要帮助。

tl;dr [P in K] allows us to define a union of keys ( K ) which may be a reduced subset of the total number of keys in T . tl;dr [P in K]允许我们定义键的并集 ( K ),它可以是T中键总数的缩减子集 Because we constrain K to be extends keyof T , we are also saying that the keys must exist on the source object of type T .因为我们将K限制为extends keyof T ,所以我们也说密钥必须存在于类型T的源 object 上。 This then allows us to use it as an index for T (eg T[P] ) in later parts of the code这允许我们在代码的后面部分将它用作 T 的索引(例如T[P]

In the example above, using Pick<Todo, 'title'>在上面的示例中,使用Pick<Todo, 'title'>

  • [P in K] = 'title' [K 中的 P] = 'title'
  • [P in keyof T] = 'title' | 'description' | 'completed' [P in keyof T] = 'title' | 'description' | 'completed' 'title' | 'description' | 'completed'

If we didn't have K , we would not be able to provide the "arguments" needed and instead would be forced to perform operations on all of the keys in T如果我们没有K ,我们将无法提供所需的“参数”,而是被迫对T中的所有键执行操作


type Pick<T, K extends keyof T> = {
  [P in K]: T[P]
}

interface Todo {
  title: string
  description: string
  completed: boolean
}

interface PickedTodo {
  title: string
}
// equivalent to Pick<Todo, 'title'>
  • T : This is our source object type (eg Todo ) from which we will be extracting keys (and their value types) that we are interested in T :这是我们的源 object 类型(例如Todo ),我们将从中提取我们感兴趣的键(及其值类型)
  • K extends keyof T : This is the set of keys ( that must be present in the source object type ) we are interested in extracting to and applying to our destination object type (eg PickedTodo ). K extends keyof T :这是一组键(必须存在于源 object 类型中),我们有兴趣提取并应用于我们的目标 object 类型(例如PickedTodo )。
    • The beauty of this syntax is that we can specify a list of keys which is a subset of the source object type's keys (ie doesn't expect all keys).这种语法的美妙之处在于我们可以指定一个键列表,它是源 object 类型键的子集(即不期望所有键)。
    • These are all valid unions which satisfy K extends keyof Todo这些都是满足K extends keyof Todo的有效联合
      • 'title'
      • 'title' | 'description'
      • 'description' | 'completed'
      • 'title' | 'description' | 'completed'
      • etc.. ETC..
  • [P in K] : In simple terms this reads as 'For every key ( P ) which exists in the union you provided ( K ), do something'. [P in K] :简单来说,这就是“对于存在于您提供的联合 ( K ) 中的每个键 ( P ),做一些事情”。
    • This is the TypeScript way of enabling us to loop over an objects keys这是使我们能够遍历对象键的 TypeScript 方法
    • In our example, PickedTodo only has one key title , ie the value of K is the following union: title在我们的示例中,PickedTodo 只有一个键title ,即K的值是以下联合: title
    • This is different to keyof Todo ( keyof T ) which would be the union of all keys on Todo , ie: 'title' | 'description' | 'completed'这与keyof Todo ( keyof T ) 不同,后者是Todo上所有键的并集,即: 'title' | 'description' | 'completed' 'title' | 'description' | 'completed'
  • T[P] : Using T as a base, extract the type of the value for each of the keys P T[P] :以T为基数,提取每个键P的值的类型
    • In our example we have already determined that there is only one key in the union K , which is title .在我们的示例中,我们已经确定联合K中只有一个键,即title Therefore we only need to perform this mapping operation once and apply to the destination object.因此我们只需要执行一次这个映射操作并应用到目的地object。
    • Our destination object receives the key title with the associated value of Todo['title'] ( T[P] ) which as we know is string我们的目的地 object 接收到键title以及Todo['title'] ( T[P] ) 的关联值,我们知道它是string
    • The resulting object is therefore interface PickedTodo { title: string; }因此,生成的 object 是interface PickedTodo { title: string; } interface PickedTodo { title: string; }

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

相关问题 在javascript中定义内置类型的方法 - define method on built-in type in javascript 什么是内置对象键类型? - What is the Built-in Object Key type called? 使用Object.prototype.toString()对内置类型进行常规类型检查 - General type checking of built-in types with Object.prototype.toString() redux 表单元素类型无效:应为字符串(对于内置组件) - redux form Element type is invalid: expected a string (for built-in components) 错误-元素类型无效:预期为字符串(对于内置组件) - ERROR - Element type is invalid: expected a string (for built-in components) Alexa内置插槽类型:无值传递 - Alexa Built-In Slot Type: No value being passed React Native,元素类型无效:需要一个字符串(对于内置组件) - React Native, element type is invalid: expected a string (for built-in components) mdbreact 错误 - 元素类型无效:需要一个字符串(对于内置组件) - mdbreact error - Element type is invalid: expected a string (for built-in components) 错误:元素类型无效:需要一个字符串(对于内置组件)-reactjs - Error: Element type is invalid: expected a string (for built-in components) - reactjs JavaScript 中是否有未定义数据类型的内置方法? - Are there any built-in methods for undefined data type in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM