[Typescript Challenges] 8. Easy - 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'
/* _____________ Your Code Here _____________ */
type If<C extends boolean, T, F> = C extends true ? T: F;
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<If<true, 'a', 'b'>, 'a'>>,
Expect<Equal<If<false, 'a', 2>, 2>>,
]
// @ts-expect-error
type error = If<null, 'a', 'b'>