Skip to content

Instantly share code, notes, and snippets.

@anztrax
Created November 30, 2025 00:46
Show Gist options
  • Select an option

  • Save anztrax/b0947779a6888bdffd57fc7e015b2d05 to your computer and use it in GitHub Desktop.

Select an option

Save anztrax/b0947779a6888bdffd57fc7e015b2d05 to your computer and use it in GitHub Desktop.
simple implementation of If in Typescript
/* _____________ 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>>,
Expect<Equal<If<boolean, 'a', 2>, 'a' | 2>>,
]
// @ts-expect-error
type error = If<null, 'a', 'b'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment