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
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]
}
<T, K extends keyof T>
T represents the type you want to work with (usually an object type or interface).K must be a subset of the keys of T (keyof T). This ensures only valid property names of T can be used.[P in K]
K.P inside K, a corresponding property will be created in the resulting type.T[P]
P, this looks up its property type from T.P = "title" and T = Todo, then T[P] = string.MyPick<T, K> constructs a new type by:
T specified in K.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 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];
};
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:
extends.K must be one or more keys of O.K.P from O.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