Skip to content

If实现

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

例如:

ts
type A = If<true, 'a', 'b'> // expected to be 'a'
type B = If<false, 'a', 'b'> // expected to be 'b'
查看解答
ts
type If<C extends boolean, T, F> = C extends true ? T : F

Released under the MIT License.