04. If
문제
Implement a utils If
which accepts condition C
, a truthy return type T
, and a falsy return type F
. C
is expected to be either true
or false
while T
and F
can be any type.
For example:
type A = If<true, 'a', 'b'> // expected to be 'a'
type B = If<false, 'a', 'b'> // expected to be 'b'
풀이
type If<C, T, F> = ?;
// 1. C는 True 또는 False 를 갖는다. 즉 Boolean 이다.제한을 걸자
type if<C extends boolean, T, F> = ?;
// 2. C 가 true라면 T 가 된다.
type if<C extends boolean, T, F> = C extends True ? T : F;
너무 간단한 문제였다. 끝!
Last updated
Was this helpful?