TypeScript's type system is Turing Complete: meaning it has conditional branching (conditional types) and works with an arbitrary huge amount of memory. As a result, you can use the type system as its own programming language complete with variables, functions, and recursion. Developers have pushed the bounds of type operations possible in the type system to write some pretty incredible things!
This blog post is a starting list of nifty things TypeScript developers have pushed the type system to be able to do. They range from binary arithmetic and rudimentary virtual machines to maze solvers and full programming languages.
✋ This Is Not Normal
Most applications try to get away with as few extreme type operations as possible. Complex logic in the type system gets unreadable and hard to debug pretty quickly. The Learning TypeScript book advises:
If you do find a need to use type operations, please—for the sake of any developer who has to read your code, including a future you—try to keep them to a minimum if possible. Use readable names that help readers understand the code as they read it. Leave descriptive comments for anything you think future readers might struggle with.
Please don't look at the following projects list and think you need to understand them to use TypeScript. These projects are ridiculous. They're the equivalent of code golf: a fun activity for a select few, but not useful for most day-to-day work.
Sorted oldest first. All projects listed come with explanations explaining how they work.
Ashley Claymore, September 2019
This well-documented set of types sets up a "virtual machine" that allows for manipulating a 4-bit stack.
VM type takes in an array of program instructions such as
"peek", and simulates manipulating the stack for each instruction.
It even comes with a fizz buzz implementation in 20 commands.
Josh Goldberg, October 2019
type Bit = 0 | 1;
This project also implements low-level bit manipulation logic in the type system. It's less extensive than the 4-bit VM, and "just" includes 8-bit integer manipulation instead.
Ronen Amiel, February 2020
This project attempts to push TypeScript's type system to its limits by actually implementing various functions and algorithms, purely on top of the type system.
Ronen made available not just a shortest-path maze solver in the type system, but also a whole suite of functions including number arithmetic, list operations, array sorting algorithms, and puzzle solving. Its puzzle solutions in particular stand out as great, well-commented uses of many type system features.
Gal Schlezinger, February 2020
This blogpost shows how to implement FizzBuzz in TypeScript using types only — with no runtime code whatsoever.
This crafty approach walks through how to set up recursive logic and number arithmetic purely in the type system. It ends with a full FizzBuzz implementation that works on numbers.
Devansh Jethmalani, August 2020
An effort to statically type xstate.
Devansh also has a fascinating type predicate eDSL project that makes heavy use of parsing in the type system:
Charles Pick, September 2020
A SQL database implemented purely in TypeScript type annotations.
The first entry in this list that extensively makes use of string parsing in the type system.
Queries are run by parsing raw SQL strings into statements that are then evaluated against the current state of the database.
SELECT statements that return type system array of objects, as well as
DELETE statements that return new database contents as a type.
Ronen Amiel, October 2020
A tiny language interpreter implemented purely in TypeScript's type-system.
Another fun project from Ronen: this time, an entire language!
It supports a Lisp-like syntax with commands such as
or, as well as functions and variables.
You have to wonder whether you could implement TypeScript itself in that language...
Josh Goldberg, October 2020
[We'll figure] how to take a template string literal, simulate a Tic Tac Toe game on it, and determine out who the winner of that game is.
This fun conference talk addition uses template literal string parsing to convert a description of game steps into a game board with pieces placed on it. Checking for a game victory is done with a set of assignability checks on the board's tuples.
Anurag Hazra, April 2022
An experimental tokenizer/parser/interpreter written entirely on type-level to push the limits of TypeScript's type system.
Each part of tokenization, parsing, and interpreting is impressive. This project takes them all to the next level by implementing a fully working language in the type system. Absolutely ridiculous.
Anurag has a collection of other great type system explorations on GitHub too: github.com/anuraghazra/type-trident.
Ronen Amiel, July 2022
Turns out it's possible to implement TypeScript's type system in TypeScript's type system 😳.
Another creation by Ronen, this is a simplified implementation of TypeScript's type system that's written in TypeScript's type annotations. It implements a surprisingly large subset of the type system: variables, functions, arrays, objects, and more!
Richard Towers, March 2023
“Okay, that looks like a correct solution, but the code is quite hard to follow, and it’s not very concise.” he asserts, wrongly.
In the form of a technical interview, Richard shows us how TypeScript's type system solves a puzzle, "N-Queens". Given an NxN chessboard, you need to find a way to place N queens on that board safely. A queen threatens another if she is in the same row, column, or diagonal.
Daniel James, July 2023
This repo contains chess implemented entirely in the (stable) Rust and Typescript type systems. Both languages feature turing complete type systems making this possible.
Near-complete chess implementations in two type systems, including castling, promotion, and en passant. TypeScript's version also includes string parsing for a Forsyth–Edwards Notation (FEN) parser.
If the explanations in projects aren't working out for you, I have two resources I'd recommend:
- The Learning TypeScript book covers the foundations of the type system up through smaller uses of conditional and inferred types.
- Zhenghao He's An Introduction To Type Programming In TypeScript is a gentle but comprehensive walkthrough of techniques used by these projects.
Additionally, the type-challenges repository on GitHub contains a wonderful suite of type challenges starting with foundations and working up to gloriously complex metaprogramming. I'd highly recommend using them to work your way up to being able to make projects like the ones in this article.
Got a project you think should be listed? Send it to @LearningTSBook on Twitter!