bh2980.dev

268 - If

  • #Type Challenges
  • #TypeScript

질문

조건 C, 참일 때 반환하는 타입 T, 거짓일 때 반환하는 타입 F를 받는 타입 If를 구현하세요. Ctrue 또는 false이고, TF는 아무 타입입니다.

예시:

type A = If<true, 'a', 'b'>  // expected to be 'a'
type B = If<false, 'a', 'b'> // expected to be 'b'

/* _____________ 테스트 케이스 _____________ */
import type { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect<Equal<If<true, 'a', 'b'>, 'a'>>, Expect<Equal<If<false, 'a', 2>, 2>>, Expect<Equal<If<boolean, 'a', 2>, 'a' | 2>>, ] // @ts-expect-error type error = If<null, 'a', 'b'>

풀이