출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md
268 - If
조건 `C`, 참일 때 반환하는 타입 `T`, 거짓일 때 반환하는 타입 `F`를 받는 타입 If를 구현하세요. `C`는 `true` 또는 `false`이고, `T`와 `F`는 아무 타입입니다.
type A = If<true, 'a', 'b'> // expected to be 'a'
type B = If<false, 'a', 'b'> // expected to be 'b'
풀이
type If<C extends boolean, T, F> = C extends true ? T : F;
extends를 이용한 조건문 연습 문제인 듯 하다.
'알고리즘 > Type Challenges' 카테고리의 다른 글
3057 - Push (0) | 2023.09.17 |
---|---|
533 - Concat (0) | 2023.09.06 |
189 - Awaited (0) | 2023.09.06 |
43 - Exclude (0) | 2023.09.06 |
18 - Length of Tuple (0) | 2023.09.06 |