myPick
FailurMan opened this issue · 3 comments
FailurMan commented
//实现ts中的内置方法pick
interface Todo {
title: string;
description: string;
completed: boolean;
}
type MyPick<T, K extends keyof T> = {
[P in K]: T[P];
};
type TodoPreview = MyPick<Todo, "title" | "completed">;
bearki99 commented
interface User {
id: number;
name: string;
password: string;
}
type MyPick<T, K extends keyof T> = {
[P in K]: T[P]
// P in K 相当于for in语法
}
type mypicker = MyPick<User, "id" | "name">;
gswysy commented
type MyPick<T, K extends keyof T> = {
[p in K]:T[p]
}
jianxingyao commented
type MyPick<T extends Record<string, any>, U extends keyof T> = {
[K in U]: T[K];
};
加了点小约束 pick 主要就是只保留传入的键类型