Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- 타입 챌린지 Deep Readonly
- 스터디 회고
- 와글와글
- 맵필로그
- 와글와글 해커톤
- TypeScript
- Type Challenge
- 타입 챌린지 IndexOf
- 타입챌린지
- 백엔드
- Mappilogue
- 타입스크립트
- 월간cs
- 노드 교과서 개정2판
- Array.prototype.push
- nestjs
- 이펙티브 타입스크립트
- 189-Awaited
- 타입 챌린지
- network
- npm
- type challenge omit
- 코딩테스트
- HTTP
- typeorm
- 타입챌린지 Readonly2
- Conditional Types
- node.js
- TypeScript 타입챌린지
- 해커톤
Archives
- Today
- Total
iamkanguk.dev
[타입챌린지-MEDIUM] 3: Omit 본문
문제
https://github.com/type-challenges/type-challenges/blob/main/questions/00003-medium-omit/README.md
풀이
type MyOmit<T, K extends keyof T> = {
[U in keyof T as U extends K ? never : U]: T[U]
}
/* _____________ 테스트 케이스 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Expected1, MyOmit<Todo, 'description'>>>,
Expect<Equal<Expected2, MyOmit<Todo, 'description' | 'completed'>>>,
]
// @ts-expect-error
type error = MyOmit<Todo, 'description' | 'invalid'>
interface Todo {
title: string
description: string
completed: boolean
}
interface Expected1 {
title: string
completed: boolean
}
interface Expected2 {
title: string
}
풀이 해설
- type MyOmit<T, K extends keyof T>: 먼저, MyOmit에서는 T와 K를 받는다. 그런데 K는 T의 key여야 한다.
- [U in keyof T as U extends K ? never : U]: T[U]
- U in keyof T는 T의 키값들을 U라는 타입 인자로 순회하겠다는 의미이다.
- as U extends K ? never : U << 이 부분이 제일 중요하다. as는 그런데라고 해석하면 된다. 해석을 쭉 해보면, U는 T의 키값이다. 그런데 만약 U가 K를 상속하면 never를 반환하고 그렇지 않으면 U를 반환한다는 뜻이다.
후기
약간 큰 그림까지는 푼 것 같은데, as 하는 부분을 몰라서 못풀었던 문제이다. 한번 더 복습해서 내 것으로 만들자!
'Type Challenge' 카테고리의 다른 글
[타입챌린지-MEDIUM] 9: Deep Readonly (0) | 2024.04.04 |
---|---|
[타입챌린지-MEDIUM] 8: Readonly 2 (0) | 2024.03.27 |
[타입챌린지-EASY] 189: Awaited (0) | 2024.03.18 |
[타입챌린지 - EASY] 14 - First of Array (0) | 2024.03.10 |
[타입챌린지 - EASY] 11 - Tuple to Object (1) | 2024.03.08 |