[英]Built-in utility type Pick explanation
Can someone explain how built-in Pick<Type, Keys> works?有人可以解释内置的 Pick<Type, Keys> 是如何工作的吗?
We have:我们有:
type Pick<T,K extends keyof T>={ // (1)
[P in K]:T[P] // (2)
}
interface Todo {
title: string
description: string
completed: boolean
}
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'>
'title'
[K 中的 P] = 'title'
'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
)。
K extends keyof Todo
这些都是满足K extends keyof Todo
的有效联合
'title'
'title' | 'description'
'description' | 'completed'
'title' | 'description' | 'completed'
[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
),做一些事情”。
title
, ie the value of K
is the following union: title
在我们的示例中,PickedTodo 只有一个键title
,即K
的值是以下联合: title
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
的值的类型
K
, which is title
.在我们的示例中,我们已经确定联合K
中只有一个键,即title
。 Therefore we only need to perform this mapping operation once and apply to the destination object.因此我们只需要执行一次这个映射操作并应用到目的地object。title
with the associated value of Todo['title']
( T[P]
) which as we know is string
我们的目的地 object 接收到键title
以及Todo['title']
( T[P]
) 的关联值,我们知道它是string
interface PickedTodo { title: string; }
因此,生成的 object 是interface PickedTodo { title: string; }
interface PickedTodo { title: string; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.