type-challenges icon indicating copy to clipboard operation
type-challenges copied to clipboard

4 - Pick

Open antfu opened this issue 5 years ago • 2 comments

✍️ Answers

Name your title starting with the question no you are trying to answer.

For example: #12 - my one-line solution

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

antfu avatar Jul 25 '20 11:07 antfu

In typescrip 4.7.4 this is not work.

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

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

/*
type test = {
    title: string;
    completed: boolean;
    invalid: unknown;
}
*/
type test = MyPick<Todo, 'title' | 'completed' | 'invalid'>

tyt34 avatar Jul 06 '23 05:07 tyt34

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

R00tx-0xf0rd avatar Nov 26 '25 11:11 R00tx-0xf0rd