If<C, T, F>
EasyP3Coding
Implement If<C, T, F>: it returns T when C is true and F when C is false. C is constrained to boolean, so If<null, 'a', 'b'> is a type error.
Tests your type must pass
type cases = [
Expect<Equal<If<true, 'a', 'b'>, 'a'>>,
Expect<Equal<If<false, 'a', 2>, 2>>,
Expect<Equal<If<true, { x: 1 }, never>, { x: 1 }>>,
]
// @ts-expect-error null is not boolean
type error = If<null, 'a', 'b'>Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
TypeScript
Run and Submit type-check your solution against the tests. ⌘Enter runs, ⌘⇧Enter submits.