00004-easy-pick

Back

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

Solution by Tnalxmsk #37774

// your answers
type MyPick<T, K extends keyof T> = { [A in K]: T[A] }

Solution by theidentify #37770

// your answers

Solution by Brahima-Fofana #37756

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

Solution by JohnGlod #37750

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

Solution by R00tx-0xf0rd #37749

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

Solution by GuoYingHAOG #37717

type MyPick<Type, Keys extends keyof Type> = {
  [Key in Keys]: Type[Key]
}

Solution by gyugod93 #37714

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

Solution by jthw1005 #37709

type MyPick<T, K> = {
  [Key in keyof T as Key extends K ? Key : never]: T[Key]
}

Solution by rauf322 #37699

type MyPick<ObjectType, SelectedKeys extends keyof ObjectType> = {
  [Key in SelectedKeys]: ObjectType[Key] 
}

Solution by ortima #37683


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

/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Expected1, MyPick<Todo, 'title'>>>,
  Expect<Equal<Expected2, MyPick<Todo, 'title' | 'completed'>>>,
  // @ts-expect-error
  MyPick<Todo, 'title' | 'completed' | 'invalid'>,
]

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

interface Expected1 {
  title: string
}

interface Expected2 {
  title: string
  completed: boolean
}

Solution by PosionEdgar #37675

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

Solution by Ju-MINJAE #37662

type MyPick<InputType extends object, Key extends keyof InputType> = {
    [P in Key]: InputType[P];
};

Solution by AlexanderNP #37642

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

Solution by jjojae9393 #37595

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

Solution by tomo-local #37569

// your answers

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

Solution by PAVANT009 #37557

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

Solution by amirdaGankhuyag #37513

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'>

const todo: TodoPreview = { title: 'Clean room', completed: false, }

Solution by amirdaGankhuyag #37512

// your answers

Solution by dzheey #37474

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

In plain words

MyPick<T, K> constructs a new type by:

  1. Taking only the properties of T specified in K.
  2. Copying both their names and their exact types into the new type.

It’s essentially a manual re-implementation of TypeScript’s built-in Pick<T, K> utility.

Solution by 3aluw #37419

// your answers
type MyPick<T, K extends keyof T> = {
  [P in K] : T[P]
} 

Solution by nyk233233 #37412

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

Solution by ivan-abdulin #37356

// 你的答案

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

Solution by lmmzss-jk #37349

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

Solution by minseonkkim #37339

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

Solution by Nakamura25257 #37255

type MyPick<T, K extends keyof T> = { 
  [P in K]: T[P];
};
  1. 泛型 K 是一个联合类型,是 T 的 key 值,通过 keyof 返回所有 key 组成的联合类型
  2. extends 泛型约束,使 K 满足条件
  3. 使用 in 遍历联合类型的成员作为key([P: string/number/symbol] 不能跟泛型类型)

Solution by djdidi #37116

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

Solution by dqks #37089

type MyPick<O extends object, K extends keyof O> = { [P in K]: O[P] };

Notes:

Solution by Dessouki98 #37050

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

Solution by Dev205Mg #37045

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

Solution by 359Steve #37010