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 |
Tags
- 코딩테스트
- 회고
- 백엔드
- TypeScript 타입챌린지
- HTTP
- configmodule
- 스터디
- 굿바이 2024년
- configservice
- 와글와글
- equal 타입
- npm
- 회고록
- 월간cs
- typeorm
- nestjs
- 2024년 회고록
- 해커톤
- microsoft azure openai
- network
- 타입챌린지
- typescript type challenge
- 타입 챌린지
- Type Challenge
- 타입스크립트
- TypeScript
- 이펙티브 타입스크립트
- type challenge equal type
- node.js
- type-safe configservice
Archives
- Today
- Total
iamkanguk.dev
[타입챌린지-MEDIUM] 3: Omit 본문
문제
https://github.com/type-challenges/type-challenges/blob/main/questions/00003-medium-omit/README.md
type-challenges/questions/00003-medium-omit/README.md at main · type-challenges/type-challenges
Collection of TypeScript type challenges with online judge - type-challenges/type-challenges
github.com
풀이
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 |