Kermani Kermani - 3 months ago 12
TypeScript Question

How is TypeScript 100.0% written in TypeScript?

In TypeScript repository on GitHub and according to GitHub, the repository just included 100.0% TypeScript (.ts files)

enter image description here

How does it work? and how TypeScript can compile itself to JavaScript just by itself?

Answer

This is called compiler bootstrapping and is common for a number of reasons, not least of which is the language you're writing is often the best-suited language for understanding the concepts you're implementing in the language you're writing...

If you take a look at the article, most major languages have self-hosting compilers (C, C++). Doing so means you're running a large amount of code through your new compiler, which is a good test of functionality. In the usual case, you're writing a compiler because you want a new language with some benefit over your current language, so being able to take advantage of those benefits while writing the compiler makes good sense.

The very first pass will have to be written in an existing language, but once you have a compiler, you can use that to compile the next revision and so on. Obviously this limits your compiler to only using features from the n-1 revision, but since you control the compiler that should be a minor issue. Quoting Wikipedia:

The main parts of the C++ compiler clang were written in a subset of C++ that can be compiled by both g++ and Microsoft Visual C++.

Since TypeScript is a superset of JavaScript, the compiler could (theoretically) be written in the shared syntax and compile under either. I don't believe that's the case here, but the relationship does give you a good starting language for the initial compiler.