“Here’s a simple coding task,” the interviewer says — “You can use any language you want”. “Any language..?”, I smirked. Little did he know that TypeScript’s type system is Turing complete...
Watch Gal reveal how to solve FizzBuzz in the type system itself, by getting the solution with no runtime code whatsoever!
TypeScript’s type system is very powerful and can express very exciting things. This technical-yet-humorous talk is done almost entirely hands-on, in an editor, inside a .d.ts file. By using a certain amount of generics, recursive types, and conditional types, we code together:
type FizzBuzzNth that resolves into a literal of “Fizz” for multiplications of 3, “Buzz” for multiplications of 5, and “FizzBuzz” for multiplications of 15. Otherwise, resolves into the given N
* A small “test framework” to test that our types work correctly * Logic constructs, such as And and If * Linked lists * …all that, without running our code once — TypeScript’s language server does it for us.