A Step Towards Compiling TypeScript to Native

Casper Beyer
Commit Log
Published in
4 min readMar 18, 2019

--

One of my pastimes is writing compilers, they’re just fun to work on. One of those projects I’ve been working for a couple of months now is a TypeScript to native compiler.

Now just to be absolutely clear here, by native I do mean actually native as in native AOT (ahead-of-time compilation) to machine code and not as another buzzword for yet another JavaScript framework.

A Quick Breakdown

The compiler is fairly simple, it’s built as a layer on top of the TypeScript compiler which adds a couple of transformations into the compilation pipeline before emitting C++ source files which are then compiled and assembled with a traditional toolchain.

Lets do a quick breakdown and highlight some of the some of the steps the compiler takes when converting from the strict subset of TypeScript to what I consider to be fairly readable and concise C++ code.

Automatic Reference Counting and Escape Analysis

So first things first, one of the more obvious and major differences between TypeScript and C++ is that the former is TypeScript is garbage collected while C++ is not.

I could have implemented this with garbage collection in C++ but an important factor to me was the readability and…

--

--

Casper Beyer
Commit Log

Indie Game Developer, Professional Software Developer and Expert Jak Shaver. Working on Deno.