Skip to content

If

约 213 字小于 1 分钟

2022-12-01

题目

Github: If

实现一个 IF 类型,它接收一个条件类型 C ,一个判断为真时的返回类型 T ,以及一个判断为假时的返回类型 FC 只能是 true 或者 falseTF 可以是任意类型。

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

解题思路

通过泛型类型约束 Cboolean , 条件类型推断 C 计算结果是否为 true

答案

type If<C extends boolean, T, F> = C extends true ? T : F

验证

import type { Equal, Expect } from '~/tc-utils'

type If<C extends boolean, T, F> = C extends true ? T : F
// ---cut---
type a = If<true, 'a', 'b'>
type b = If<false, 'a', 'b'>

type cases = [
  Expect<Equal<a, 'a'>>,
  Expect<Equal<b, 'b'>>,
]

参考