Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

myPick #339

Open
FailurMan opened this issue Dec 30, 2022 · 3 comments
Open

myPick #339

FailurMan opened this issue Dec 30, 2022 · 3 comments

Comments

@FailurMan
Copy link
Contributor

FailurMan commented Dec 30, 2022

//实现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">;
@FailurMan FailurMan changed the title 实现myPick myPick Dec 30, 2022
@bearki99
Copy link

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
Copy link

gswysy commented Mar 13, 2024

type MyPick<T, K extends keyof T> = {
    [p in K]:T[p]
}

@jianxingyao
Copy link

type MyPick<T extends Record<string, any>, U extends keyof T> = {
[K in U]: T[K];
};

加了点小约束 pick 主要就是只保留传入的键类型

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants