📌 문제
https://github.com/type-challenges/type-challenges/blob/main/questions/00004-easy-pick/README.ko.md
type-challenges/questions/00004-easy-pick/README.ko.md at main · type-challenges/type-challenges
Collection of TypeScript type challenges with online judge - type-challenges/type-challenges
github.com
T에서 K 프로퍼티만 선택해 새로운 오브젝트 타입을 만드는 내장 제네릭 Pick<T, K>을 이를 사용하지 않고 구현하기 !!
😭 나의 실수
우와 보자마자 짱 쉽다 하고 풀었는데 .. 뭐지? 왜 description에 never가 생기지 싶었다.
전체 k를 순회를 돌게 되어 description은 never라는 타입을 갖게 된 것 !
type MyPick<T, K> = { [k in keyof T]: k extends K ? T[k] : never };
{
title: string;
description: never;
completed: boolean;
}
뿐만 아니라 K는 문제에 나와있든, T의 일부이기 때문에 타입을 T의 일부로서 줄여주는 것이 좋다.
✅ 나의 실수
type MyPick<T, K extends keyof T> = { [P in K]: T[P] };
K를 T의 key라고 타입을 좁히고 K에 대해서만 돌면서 빼주면 끝 !..
'TypeScript' 카테고리의 다른 글
| 친해지기3-Type Challenges : 00011-easy-tuple-to-object (0) | 2025.04.30 |
|---|---|
| 친해지기2-Type Challenges : 00007-easy-readonly (1) | 2025.04.29 |
| [TS] 타입 가드 & 타입 내로잉(is, as const, keyof, typeof, as) (0) | 2025.04.23 |
| [TS] 타입스크립트에서의 클래스 (0) | 2025.04.23 |
| [TS] TIL , as const ( const assertion ) (0) | 2025.04.22 |