98

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?

4
  • 27
    Well, after the first version, you have a compiler and can use that to compile the next compiler, and so on, no?
    – ssube
    Commented Aug 30, 2016 at 19:29
  • 20
    This is how a programmer asks: "What came first, the chicken or the egg?" Commented Dec 8, 2017 at 9:53
  • 1
    @ssube You said _after the first version_. So what language was used to write 1st version? I mean the very first compilation.
    – AGupta
    Commented Jul 11, 2019 at 14:35
  • 1
    Same way Java is primarily written in Java. At it's very core is a basic set of native libs that tie into the VM and the Java programming language builds onto there basic capabilities to create more complex objects. This way you only have to modify these basic low-level libraries to create ports for different systems rather than porting the entire language over.
    – JSON
    Commented Jul 12, 2019 at 18:14

4 Answers 4

102

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.

3
  • But, according to the article, it still needs some of the native languages (here JavaScript) to compile some part of the superset of JavaScript (typeScript). And in Github there are some js files too... Commented Sep 19, 2016 at 6:10
  • 2
    @Kermani but since Typescript is a superset of JavaScript, you can write a valid JS program in TS, compile it, then use it to compile a TS program using more language features, and so on. Technically speaking, it could be TS all the way down. Most other transpilers aren't strict supersets (think of C and C++), so they can't take advantage of that quite so much.
    – ssube
    Commented Sep 20, 2016 at 15:22
  • 4
    My understanding is that: TS is superset of JS ==> JS is TS ==> Any JS code is also TS code ==> TS compiler is 100% written in TS.
    – themefield
    Commented Jul 29, 2018 at 19:01
66

I just want to add something that I think is interesting.

In git you can have a file called .gitattributes. Github has a project called linguist which can make use of that file for their language details section in every repository. In the typescript repo there is a .gitattributes file which has the following content:

*.js linguist-language=TypeScript
* -text

You can fork the typescript repo, remove that file, commit to github and wait for some time while their repository analysis job completes and the language graph will change.typescript repo language stats without gitattributes

2
  • 8
    That's fantastic answer, I guess many people who might come to this question, had no idea about this file. Maybe you could also share the link to the Typescript forked repository without that specific file Commented Mar 28, 2020 at 13:50
  • I forked the repo just to test what will happen when I delete that file. After I saw the result I've deleted it, since I am not going to contribute anything to it.
    – Ivan Ruski
    Commented Mar 28, 2020 at 16:56
26

Typescript is self-hosting and maintains a Last-Known-Good (LKG) version of itself to compile the next version. Currently (30/08/2016) that version is in the lib directory.

2
  • Last-known-good, - fine! But how did they implement the initial version? Which language/compiler did they use? C? Commented Jul 27, 2021 at 6:35
  • 2
    @JerryGreen JavaScript I'm pretty sure
    – Jacob
    Commented Apr 10, 2022 at 18:34
0

First ts->js transpiler was written in javascript (or could be written in any other language). After that you had working transpiler. That allows you to build any program in pure typescript. Even transpiler.

So the second version of transpiler could be written in typescript. So since second iteration all next versions of ts->js transpilers were built in typescript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.