Skip to content

Instantly share code, notes, and snippets.

@drodsou
Last active March 21, 2022 02:00
Show Gist options
  • Save drodsou/670f1d800799faf089e3b50f936b46ea to your computer and use it in GitHub Desktop.
Save drodsou/670f1d800799faf089e3b50f936b46ea to your computer and use it in GitHub Desktop.
Rock paper scissors in 4 lines of typescript types (no javascript)

Rock paper scissors in 4 lines of typescript types (no javascript)

Thanks to @harrysolovay

type Rock = { b?: never, c?: unknown }
type Paper = { a?: unknown, c?: never }
type Scissors = { a?: never, b?: unknown }

type Beats<A extends B, B> = {} 

let a: Beats<Rock, Paper>     // will throw error eg in Beats<Rock, Paper>, but not in Beats<Paper, Rock>

Try it typescript playground

Turing complete

An amazing concept is that typescript types are Turing complete, so you can do jawdropping things like an SQL database implementation in just typescript types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment