A Step Towards Compiling TypeScript to Native
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…