Language/Typescript
타입챌린지 : 43-Exclude (easy)
hsloth
2023. 3. 2. 17:31
이 글은 제가 타입챌린지를 하면서 해석한 내용을 적는 글입니다. 틀린 내용이 있으면 댓글 달아주시면 감사하겠습니다.
https://github.com/type-challenges/type-challenges/blob/main/questions/00043-easy-exclude/README.md
GitHub - type-challenges/type-challenges: Collection of TypeScript type challenges with online judge
Collection of TypeScript type challenges with online judge - GitHub - type-challenges/type-challenges: Collection of TypeScript type challenges with online judge
github.com
type MyExclude<T, U> = T extends U ? never : T;
// test cases
type cases = [
Expect<Equal<MyExclude<'a' | 'b' | 'c', 'a'>, 'b' | 'c'>>,
Expect<Equal<MyExclude<'a' | 'b' | 'c', 'a' | 'b'>, 'c'>>,
Expect<Equal<MyExclude<string | number | (() => void), Function>, string | number>>,
]
type MyExclude : MyExclude 라는 타입을 정의한다.
<T, U> : T와 U를 제네릭 인자로 사용한다.
T extends U ? never : T : extends는 === 이라고 생각하자. T와 U가 같으면 never(null과 비슷한 개념이라고 생각하자 다만, 실제 값은 들어가지 않는다)를 리턴하고 아니라면 T를 리턴한다.