Introduction to Typescript
The internet consists of technology that has transformed the way humans interact with computers and each other. One of the main drivers behind the growth of the web was the introduction of the web browser. The browser was originally intended for browsing "static" documents (interlinked by hyperlinks). This was the beginning of the technology revolution that we call the World Wide Web. The next feature that we wanted was to add interactivity to it. We needed a programming language for the browser and the lead to the creation of JavaScript. Over time, and the standard language for the Web has become JavaScript. (Even though JavaScript is an extremely interesting language we cannot cover the entire history of JavaScript here, so we will save that for another day.)
With it's history as an early contender for the role of the programming language for the web, JavaScript has now emerged as the only viable language that is supported by all the existing browsers. JavaScript is an interesting language with many good parts but some bad parts. JavaScript's greatest strength is also its greatest weakness. The fact that it is everywhere on the web - it's greatest strength,also means that it is hard to evolve JavaScript to fix any of its bad parts - it's greatest weakness.
Rather than go into all of the strengths and weaknessed of Javascript let us focus here on two main issues that we need to tackle if we want to build rich interactive applications (large programs) for the web based on JavaScript. They are:
- Support for Scaling a Dynamic language:As programs get larger the lack of static types becomes a problem. (This is a very subjective comment but I hope you can see why this can be true). It is hard to spot errors, refactor and understand large programs written in a dynamically type language. A language with support for static typing and type inference which transpiles into JavaScript would be a way to go forward.
- Fix\/hide fundamental flaws in the JavaScript language: Since it is hard to change JavaScript itself quickly, this problem can be tackled by writing a new language that fixes existing problems in JavaScript (scoping, classes, modules and namespaces etc) and supports new language features consistent with the nature and evolution of JavaScript. If the "new" language is statically typed it can enable the creation of tools and practices that increase developer productivity (static checking, statement completion, tool tips etc.). The code from this new language is then transpiled into JavaScript. This transpiled code would ensure compliance with current and future JavaScript standards.
TypeScript represents an effort lead by Anders Hejlsberg to create a language that does what we just discussed. TypeScript syntax is a super set of ECMAScript 2015 (ES2015) syntax. All JavaScript programs are valid TypeScript programs which facilitates an easy migration and transition path. But Typescript goes much further to provide many features that we would want to have in a language that we would use going forward to "program the Web".
Having made all these arguments, the best way forward is to test TypeScript and draw your own conclusions about the language. There are other languages based on similar ideas like CoffeScript, Dart and Elm among others. To get a glimpse of the different attempts that are out there take a look at this list.
If you are interested at a very different attempt to tackle this problem based on functional programming ideas you could try Elm. I have written a very similar book about Elm here.
Static Typing and type inference in a programming language
This is a topic that can quickly become a religious debate so I will just touch on a couple of points here. As we go through this book I will highlight more features of a statically typed language with type inference that will make you a more productive programmer and your programs more robust.
The compiler as your friend For those who that grown up using statically typed languages this is obvious. But if you did much of your programming using a dynamically typed language then this is something that you discover gradually. since dynamic languages use types as well. You just have less control over specifying types in a dynamic language. The idea that taking time to use and define types as you write your programs will give you tremendous benefits as your program grows in size and complexity is something that I think each person should explore and discover. This is better than engaging in a religious debate about whether we need static types. The compiler uses this type information you provide to give you valuable inputs about possible errors in your program. Ensuring that there are no errors that your compiler can detect become a valuable first step towards ensuring that your program is correct. This does not mean that you will have no errors or that you do not need to test your program, but using static type checking is a valuable step that will eliminate many errors in your code.
Type inference Now once you begin using types you notice that sometimes the type declarations get in the way. You need to work out the types and input that into your editor. This is especially noticeable when you get to using Generics and other more advanced features of a language. So now we get to this idea of using type inference, where the compiler can infer the types for you and use this information. This is not "AI for types" but it uses information in your code to infer the types that best support what you have written. Of course, you can override the inference by providing the compiler with the types you intend to use.
Better tooling with type inference Once we have a language that has a type inference mechanism, then we do not have to wait for us to compile the program to detect errors. Combining type inference with a good editor we can provide statement completion, intellisense and other valuable information that can make a programmer much more productive while writing code.
In my experience, this combination of static types and type inference is a powerful feature that any new language should have. We can and should go further than this by providing support for code contracts and other declarative methods to ensure that we can leverage the compiler to do as much checking for us as possible. Then we can supplement this with a set of tests for functionality using a good testing framework. It is not a question of Types versus Tests , it is using Types and Tests together that gets us the best results.
Now, that we have all this information as background, let us proceed to explore TypeScript.