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
// your answers
Solution by shuhrat-rizaev04 #36995
type MyPick<T, K extends keyof T> = {
[P in K]: T[P];
}
Solution by mag123c #36990
// 你的答案
type MyPick<T, K extends keyof T> = { [key in K] : T[key] }
Solution by songzhelei1109 #36984
// your answers
Solution by matmon12 #36980
// 你的答案
type MyPick<T, K extends keyof T> = {
[P in K]:T[P]
}
Solution by viulu5824 #36979
type MyPick<T, K extends keyof T> = { [P in K]: T[P]}
Solution by YiShengYouNi #36969
// your answers
type MyPick<T, K extends keyof T> = {
[key in K]:T[key]
}
Solution by Anonymous961 #36958
解题思路
要想实现从类型 T 中选出符合 K 的属性,就需要遍历T,在T中找出K对应的属性,并返回一个新类型
也就是说,如何实现遍历是关键,主要使用 in 和 keyof 这两个关键字,遍历方法如下
type Copy<T> = {
[P in keyof T]: T[P];
};
type Case = Copy<{
a: string;
b: number;
}>;
let obj: Case = {
a: "1",
b: 1,
};
另外,还需要注意下错误的测试用例MyPick<Todo, "title" | "completed" | "invalid">,我们必须限制K对应的这一系列属性在T中存在
题解
type MyPick<T, K extends keyof T> = {
[P in K]: T[P];
};
心得与知识点
in 和 keyof的使用 来实现遍历Solution by lkwavestian #36943