00004-easy-pick

Back

// 여기 풀이를 입력하세요

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

Solution by nave-nurihaus #38182

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

type TodoPreview = {
  title: Todo['title']
  completed: Todo['completed']
}

Solution by nirrie #38169

// 你的答案

/* _____________ 你的代码 _____________ */

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

/* _____________ 测试用例 _____________ */ 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 qiao-yang #38164

// 解答をここに記入
type MyPick<T, K extends keyof T> = {
  [P in K]:T[P]
}

Solution by yko-git #38142

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

keyof T -> returns a union of all property keys of T K & keyof T -> returns the set keys that are in both K and keyof T

This version avoids using K extends keyof T, so it does not enforce that K must be valid keys of T. Instead, invalid keys are silently removed from the resulting type, rather than producing a type error.

Solution by Milimas #38132


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

Solution by riyajath-ahamed #38130

/* _____________ 你的代码 _____________ */

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

Solution by Tofu-Xx #38113

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

Solution by NhanPhan159 #38102

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

Solution by StoyanStoyanov1 #38091

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

Solution by mainakgupta33 #38064

type T={
    [key:string]:any
}
type picks<T extends object,K extends keyof T>=
{
    [P in K]: T[P];
}

Solution by anilshresth #38056

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

Solution by ahm4dd #38043

type New = MyPick<Food, 'stale' | 'name'>

Solution by Nikita0x #38020

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

Solution by Maleeran #38005

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

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

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

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

  1. keyof类型操作符: 获取T[todo]的对象属性名字
  2. 通过 K extends: 来约束属性比如从 keyof中
  3. 通过in类型操作符:映射类型, 遍历联合类型【K】的每个成员
  4. 返回T[key]

Solution by xingdongyu1994 #37994

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

Solution by seungjae0619 #37983

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

Solution by sohan9819 #37980

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

Solution by dubovea #37970

// 解答をここに記入
type MyPick<T, K extends keyof T> = {
  [k in K] : T[k]
}

Solution by tac-tac-go #37902

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

Solution by LoveDreaMing #37900

/* _____________ Your Code Here _____________ */

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

/* _____________ 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 tuannvnguyen #37866

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

Solution by istamal #37846

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

Solution by Hansol46 #37829

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

Solution by pkutsenko #37818

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




Solution by liuyee12 #37786

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

Solution by jisooooooooooo #37778

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