00004-easy-pick

Back

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

Solution by HelloWook #35833

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

Solution by reonce #35818

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

Solution by song4613107 #35782

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

Solution by song4613107 #35781

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

Solution by hatbann #35756

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 }

Solution by deyaa-pozan #35751

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

Solution by Parmerlee #35749

// 你的答案

//使用交叉类型 将不属于T的键值剔除掉 type MyPick<T, K extends string | symbol> = { [key in K & keyof T]: T[key] };

Solution by samecos #35745

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

Solution by XenoPOMP #35738

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

Solution by Sensuele #35734

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

Solution by Yangkro #35709

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