Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 907
Mohammad Kermani Avatar asked Aug 30 '16 19:08

Mohammad Kermani


People also ask

Is TypeScript compiler written in TypeScript?

The TypeScript compiler is itself written in TypeScript and compiled to JavaScript. It is licensed under the Apache License 2.0. TypeScript is included as a first-class programming language in Microsoft Visual Studio 2013 Update 2 and later, alongside C# and other Microsoft languages.

What is TypeScript written in?

TypeScript is JavaScript with syntax for types. TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.

How is TypeScript code compiled?

TypeScript provides a command-line utility tsc that compiles (transpiles) TypeScript files ( . ts ) into JavaScript. However, the tsc compiler (short for TypeScript compiler) needs a JSON configuration file to look for TypeScript files in the project and generate valid output files at a correct location.

What does TSC stand for TypeScript?

The TypeScript Compiler (also known as "tsc") is the backbone of many bundlers, plugins, and tools that work with TypeScript.


1 Answers

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.

like image 79
ssube Avatar answered Oct 07 '22 00:10

ssube