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
// your answers
type MyPick<T, K extends keyof T> = {
[Key in K]: T[Key];
}
Solution by AlexBraunMagic #36921
type MyPick<T extends Record<any,any>, K extends keyof T> = {
[key in K]: T[key]
}
Solution by tobi-ademola #36883
type MyPick<T, K extends keyof T> = { [P in K]: T[P]}
Solution by shaishab316 #36837
type MyPick<T, K extends keyof T> = {
[P in K]: T[P]
} //Here we need change from any to this, because we create a new type containing only those fields from T that are specified in K, preserving their types.
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 GormanProg123 #36812
type MyPick<T, K extends keyof T> = {
[Key in K]: T[Key];
}
Solution by BernTheSeal #36807
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 AnastasiaSv #36758
type MyPick<T, K extends keyof T> = {
[P ]: T[P]
};
Solution by seroak #36739
type MyPick<T, K extends keyof T> = {
[P in K]: T[P]
};
Solution by seroak #36738
// your answers
```type MyPick<T, K extends keyof T> = {
[P in K]: T[P];
};
Solution by VitaliyTheKeyboardist #36687
// your answers
type MyPick<T, K extends keyof T> = {
[key in K]: T[key]
}
Solution by desertby #36639
type MyPick<T, Y extends keyof T> = Omit<T, keyof Omit<T, Y>>
Solution by grishbar #36634
type MyPick <T, K extends keyof T> = {
[P in K]: T[P]
}
Solution by kimdaeyeob #36618