Book Image

Learning TypeScript 2.x - Second Edition

By : Remo H. Jansen
Book Image

Learning TypeScript 2.x - Second Edition

By: Remo H. Jansen

Overview of this book

TypeScript is an open source and cross-platform statically typed superset of JavaScript that compiles to plain JavaScript and runs in any browser or host. This book is a step-by-step guide that will take you through the use and benefits of TypeScript with the help of practical examples. You will start off by understanding the basics as well as the new features of TypeScript 2.x. Then, you will learn how to work with functions and asynchronous programming APIs. You will continue by learning how to resolve runtime issues and how to implement TypeScript applications using the Object-oriented programming (OOP) and functional programming (FP) paradigms. Later, you will automate your development workflow with the help of tools such as Webpack. Towards the end of this book, you will delve into some real-world scenarios by implementing some full-stack TypeScript applications with Node.js, React and Angular as well as how to optimize and test them. Finally, you will be introduced to the internal APIs of the TypeScript compiler, and you will learn how to create custom code analysis tools.
Table of Contents (17 chapters)

The TypeScript architecture

In this section, we will focus on TypeScript's internal architecture and its original design goals.

Design goals

The following list describes the main design goals and architectural decisions that shaped the way the TypeScript programming language looks today:

  • Statically identify JavaScript constructs that are likely to be errors: The engineers at Microsoft decided that the best way to identify and prevent potential runtime issues was to create a strongly-typed programming language and perform static type checking at compile time. The engineers also designed a language services layer to provide developers with better tools.
  • High compatibility with existing JavaScript code: TypeScript is a superset of JavaScript; this means that any valid JavaScript program is also a valid TypeScript program (with a few small exceptions).
  • Provide a structuring mechanism for larger pieces of code: TypeScript adds class-based object-orientation, interfaces, namespaces, and modules. These features will help us to structure our code in a much better way. We will also reduce potential integration issues within our development team and our code will become easier to maintain and scale by adhering to the best object-oriented principles and recommended practices.
  • Impose no runtime overhead on emitted programs: It is common to differentiate between design time and execution time when thinking about TypeScript. We use the term design time or compile time to refer to the TypeScript code that we write while designing an application, while we use the term execution time or runtime to refer to the JavaScript code executed after compiling some TypeScript code.

TypeScript adds some features to JavaScript, but those features are only available at design time. For example, we can declare interfaces in TypeScript, but since JavaScript doesn't support interfaces, the TypeScript compiler will not declare or try to emulate this feature at runtime (in the output JavaScript code).

The Microsoft engineers provided the TypeScript compiler with some mechanisms, such as code transformations (converting TypeScript features into plain JavaScript implementations) and type erasure (removing static type notation), to generate clean JavaScript code. Type erasure removes not only the type annotations, but also all the TypeScript-exclusive language features such as interfaces.

Furthermore, the generated code is highly compatible with web browsers as it targets the ECMAScript 3 specification by default, but it also supports ECMAScript 5 and ECMAScript 6. In general, we can use the TypeScript features when compiling to any of the available compilation targets, but sometimes some features will require ECMAScript 5 or a higher version as the compilation target.

  • Align with current and future ECMAScript proposals: TypeScript is not just compatible with existing JavaScript code; it is also compatible with some future versions of JavaScript. At first glance, we may think that some TypeScript features make it quite different from JavaScript, but the reality is that all the features available in TypeScript (except the type system features) follow the ECMAScript proposals, which means that many of the TypeScript files will eventually be available as native JavaScript features.
  • Be a cross-platform development tool: Microsoft released TypeScript under the open source Apache license and it can be installed and executed in all major operating systems.

TypeScript components

The TypeScript language has three main internal layers. Each of these layers is, in turn, divided into sublayers or components. In the following diagram, we can see the three layers (three different shades of gray) and each of their internal components (boxes):

In the preceding diagram, the acronym VS refers to Microsoft's Visual Studio, which is the official family of integrated development environments (IDEs) for all Microsoft products (including TypeScript). We will learn more about this and the other IDEs in Chapter 9, Automating Your Development Workflow.

Each of these main layers has a different purpose:

  • Language: Features the TypeScript language elements.
  • Compiler Performs the parsing, type checking, and transformation of your TypeScript code to JavaScript code.
  • Language services: Generates information that helps editors and other tools provide better assistance features, such as IntelliSense or automated refactoring.
  • IDE integration (VS Shim): The developers of the IDEs and text editors must perform some integration work to take advantage of the TypeScript features. TypeScript was designed to facilitate the development of tools that help to increase the productivity of JavaScript developers. Because of these efforts, integrating TypeScript with an IDE is not a complicated task. A proof of this is that the most popular IDEs these days include good TypeScript support.
In other books and online resources, you may find references to the term transpiler instead of compiler. A transpiler is a type of compiler that takes the source code of a programming language as its input and outputs the source code into another programming language with a similar level of abstraction.

We will learn more about the TypeScript language services and the TypeScript compiler in Chapter 15, Working with the TypeScript Compiler and the Language Services.