00004-easy-pick

Back

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

Solution by aaronplanell #35531

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

Solution by thukyaw11 #35523

// ============= Test Cases =============
import type { Equal, Expect } from './test-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
}


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

Solution by hawkWang98 #35501

// 여기 풀이를 입력하세요
type MyPick<T, K extends keyof T> = { [key in K]: T[key] }

Solution by Gwanghun-Im #35487

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

Solution by gangnamssal #35464

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

Solution by htpple #35440

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

Solution by RanungPark #35428

// your answers
interface MyType {
    id: number;
    name: string;
    addr1: string;
}

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

const myData: ModType<MyType, "id" | "name"> = {
    id: 1,
    name: "xyz",
};

Solution by Sathiyapramod #35408

// your answers

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

Solution by yyliuliang #35400

// 你的答案
type MyPick<T, K extends keyOf T> = {
  [key in K]: T[key]
}

type TodoPreview = MyPick<Todo, 'title' | 'completed'>;

Solution by dixiaioping #35381

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

TypeScript Playground answer

Solution by SB-SLIM #35374

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

Solution by gyeounjeong #35349

// your answers

Solution by DongEsssss #35348

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

Solution by yxiaoli #35334

// your answers

Solution by serg310583 #35310

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

Solution by Muntazir-sd #35190

/* 4 - Pick

by Anthony Fu (@antfu) #easy #union #built-in

Question

Implement the built-in Pick<T, K> generic without using it.

Constructs a type by picking the set of properties K from T

For example:

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

type TodoPreview = MyPick<Todo, 'title' | 'completed'>

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

View on GitHub: https://tsch.js.org/4 */

/* _____________ Your Code Here _____________ */

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

/* _____________ Test Cases _____________ */ 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 }

/* _____________ Further Steps _____________ / /

Share your solutions: https://tsch.js.org/4/answer View solutions: https://tsch.js.org/4/solutions More Challenges: https://tsch.js.org */

Solution by leetflow #35153

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

Solution by JAGORING #35144

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

Solution by lwfxzz #35114

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

Solution by chenweiHuabi #35058

// 你的答案

Solution by chenweiHuabi #35057

// 你的答案

type MyPicker <T,K extents keyof T> = { [key in K]:T[key] }

Solution by Sfhh25 #34968

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

// 你的答案

Solution by Xiaofan-song #34958

// 你的答案
type MyPick<T, K extends keyof T> = {
  [P in K]: T[P]
}

Solution by zhaiweile #34952

type MyPick<T, K extends keyof T> = {
  [key in keyof T as key extends K ? key : never]: T[key];
};
  1. Declares K type (must be subset of the keyof T)
  2. key in keyof T as means mapped type all the iterates over all keys in T.
  3. key extends K ? key : never : is conditional type
  1. : T[key] : preserves the original type of each selected property from T.

Solution by miju-Park #34915

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

Solution by jsk3342 #34893

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

Solution by 56aiden90 #34865

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

Solution by PrahladMehta #34843

// your answers

Solution by PrahladMehta #34842

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

Solution by HrOkiG2 #34828