Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
background
XDM, look what I found, like I said at the beginning, little knowledge, big challenge! This article is participating in the programmer’s essential knowledge, I really not for the reward, just update two hydrology in one day, purely for learning, learning makes me happy, you remember to believe me! Ahem! Last time his handwritten Record, do not know you see officer satisfaction (although I know there is no one to see), nonsense not to say, really not to reward, we next look at the legend of Pick, the literal meaning is to choose! See how it picks its way into our learning journey
The body of the
The official example
First look at the official example, as well as the explanation, to understand the good implementation
Constructs a type by picking the set of properties Keys (string literal or union of string literals) from Type.
interface Todo {
title: string;
description: string;
completed: boolean;
}
type TodoPreview = Pick<Todo, "title" | "completed">;
const todo: TodoPreview = {
title: "Clean room".completed: false}; todo;Copy the code
The original Todo interface had three fields, balabalabala, and now the requirement is, I don’t want description anymore, but I don’t want to change the original interface. I just took two of the titles and completed (don’t worry, there was a new Utility Type that did the reverse) and there was Pick! Here XDM is paying attention to a detail, because it is Pick, so naturally we Pick the key is the original interface of the key, this is also the implementation we need to consider, now to implement the Pick function
Begin to implement
Same old nonsense literature, we see that Pick needs to pass in two generics, so the shelf is natural
type MyPick<T, K> = {
}
Copy the code
T is the type that we pass in, K is the key that we pick, and then we’re going to implement it, and the key that we pick must be the key that was in the interface before, so keyof and extends
type MyPick<T, K extends keyof T> = {
}
Copy the code
I feel like I’m more than halfway there, and then of course I iterate over the selected keys and mark them with their original type
type MyPick<T, K extends keyof T> = {
[P in K]: T[P]
}
Copy the code
Implementation is good to see if the source code and we are the same
The source code to explain
Direct source code
/** * From T, pick a set of properties whose keys are in the union K */
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
Copy the code
It is exactly the same, I am really a little genius, the implementation process and steps have been explained in the above link, here will not repeat! I hope XDM can be satisfied with it and give me more support. I hope you can discuss it with me. If there is any explanation that is not quite correct, I also hope you can give me some guidance. Thanks for watching, today’s hydrology is really over!
reference
- TS Utility Types document – Pick