00004-easy-pick

Back

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 chenweiCwCw #35058

// 你的答案

Solution by chenweiCwCw #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

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

Solution by eunsukimme #34805

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

Solution by heryTz #34794

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

Solution by Kim-Ji-Seop #34790

문제 설명

T에서 K 프로퍼티만 선택해 새로운 오브젝트 타입을 만드는 내장 제네릭 Pick<T, K>을 이를 사용하지 않고 구현하세요.

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

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

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

Pick 타입이란 ?

타입스크립트의 유틸리티 타입 중 하나로, 유틸리티 타입은 이미 정의해 놓은 타입을 변환할 때 유용한 타입이다.

Pick 타입은 특정 타입에서 몇 개의 속성을 선택하여 타입을 정의할 수 있으며 Pick<Type, Keys> 형식으로 작성할 수 있다. 이때, keys 값에는 문자열 리터럴 혹은 유니온 형식의 문자열 리터럴이 올 수 있다.

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

// Todo타입에서 'title', 'completed' 속성을 선택하여 타입 정의
type TodoPreview = Pick<Todo, 'title' | 'completed'>

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

풀이

Pick 타입을 유틸리티 타입을 사용하지 않고 직접 구현하는게 문제이므로 가져온 타입을 T, 해당 타입의 키를 확장하는 K를 선언하여 제네릭에서의 extends를 통해 타입의 조건부 제한을 설정하고 Mapped Type을 사용하여 타입을 정의합니다.

맵드 타입(Mapped Type)이란? 기존에 정의되어 있는 타입을 새로운 타입으로 변환해 주는 문법을 의미합니다.

// 제네릭 extends 활용으로 타입 제한 설정
type MyPick<T, K extends keyof T> = {
  // Mapped type in 연산자를 통해 K 집합 타입의 요소들을 하나씩 치환
   [P in K]: T[P];
}

Solution by MyeonghoonNam #34783

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

Solution by pomeranian91 #34767

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

Solution by kang-kibong #34740

// your answers

Solution by AdrienPiombini #34733

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

Solution by ainochi-kor #34723

``<!-- 小贴士:

🎉 恭喜你成功解决了挑战,很高兴看到你愿意分享你的答案!

由于用户数量的增加,Issue 池可能会很快被答案填满。为了保证 Issue 讨论的效率,在提交 Issue 前,请利用搜索查看是否有其他人分享过类似的档案。

你可以为其点赞,或者在 Issue 下追加你的想法和评论。如果您认为自己有不同的解法,欢迎新开 Issue 进行讨论并分享你的解题思路!

谢谢! -->

`// 你的答案

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 1710929851 #34713

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

Solution by lephuthuc2001 #34694

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

Solution by Hailey0930 #34686

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

Solution by shuvro111 #34684

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

Solution by raeyoung-kim #34679

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

处理这个问题,令我加深了对泛型、继承、keyof、in的理解👍

Solution by oOzhuoxiaOo #34669

type MyPick<T, K> = K extends keyof T ? T[K] : never;

Solution by nathan2slime #34652