Skip to main content
main-content
Top

About this book

Work with Typescript and get the most from this versatile open source language. Author Adam Freeman begins this book by describing Typescript and the benefits it offers, and goes on to show you how to use TypeScript in realistic scenarios, going in-depth to give you the knowledge you need.
Starting from the nuts-and-bolts and building up to the most advanced and sophisticated features, you will learn how TypeScript builds on the JavaScript type system to create a safer and more productive development experience and understand how TypeScript can be used to create applications using popular frameworks, including Node.js, Angular, React, and Vue.js.
Each topic is covered clearly and concisely and is packed with the details you need to learn to be truly effective. The most important features are given a no-nonsense in-depth treatment and chapters include common problems and details of how to avoid them.

What You Will LearnGain a solid understanding of the TypeScript language and toolsUse TypeScript for client- and server-side developmentExtend and customize TypeScriptDebug and unit test your TypeScript code
Who This Book Is For
Developers who want to start using TypeScript, for example to create rich web applications using Angular, React, or Vue.js

Adam Freeman is an experienced IT professional who has held senior positions in a range of companies, most recently serving as chief technology officer and chief operating officer of a global bank. Now retired, he spends his time writing and long-distance running.

Table of Contents

Getting Started with TypeScript

Frontmatter

Chapter 1. Your First TypeScript Application

Abstract
The best way to get started with TypeScript is to dive in. In this chapter, I take you through a simple development process to create an application that keeps track of to-do items. Later chapters show how TypeScript features work in detail, but a simple example will be enough to demonstrate how the basic TypeScript features work. Don’t worry if you don’t understand everything in this chapter. The idea is just to get an overall sense of how TypeScript works and how it fits into an application.
Adam Freeman

Chapter 2. Understanding TypeScript

Abstract
TypeScript is a superset of the JavaScript language that focuses on producing safe and predictable code that can be executed by any JavaScript runtime. Its headline feature is static typing, which makes working with JavaScript more predictable for programmers familiar with languages such as C# and Java. In this book, I explain what TypeScript does and describe the different features it provides.
Adam Freeman

Chapter 3. JavaScript Primer, Part 1

Abstract
Effective TypeScript development requires an understanding of how JavaScript deals with data types. This can be a disappointment to developers who adopt TypeScript because they found JavaScript confusing, but understanding JavaScript makes understanding TypeScript easier and provides valuable insights into what TypeScript offers and how its features work. In this chapter, I introduce the basic JavaScript type features, continuing with more advanced features in Chapter 4.
Adam Freeman

Chapter 4. JavaScript Primer, Part 2

Abstract
In this chapter, I continue describing the JavaScript type features that are important to TypeScript development. In this chapter, I focus on the JavaScript support for objects, the different ways they can be defined, and how they relate to JavaScript classes. I also demonstrate the features for handling sequences of values, the JavaScript collections, and the modules feature, which allows a project to be split up into multiple JavaScript files.
Adam Freeman

Chapter 5. Using the TypeScript Compiler

Abstract
In this chapter, I show you how to use the TypeScript compiler, which is responsible for transforming TypeScript code into JavaScript that can be executed by browsers or the Node.js runtime. I also describe the compiler configuration options that are most useful for TypeScript development, including those that are used with the web application frameworks covered in Part 3 of this book.
Adam Freeman

Chapter 6. Testing and Debugging TypeScript

Abstract
In this chapter, I continue the theme of TypeScript development tools started in Chapter 5, which introduced the TypeScript compiler. I show you the different ways that TypeScript code can be debugged, demonstrate the use of TypeScript and the linter, and explain how to set up unit testing for TypeScript code.
Adam Freeman

Working with TypeScript

Frontmatter

Chapter 7. Understanding Static Types

Abstract
In this chapter, I introduce the key TypeScript features for working with data types. The features I describe in this chapter are the foundations for working with TypeScript, and they are the building blocks for the advanced features described in later chapters.
Adam Freeman

Chapter 8. Using Functions

Abstract
In this chapter, I explain how TypeScript is applied to functions, showing you how TypeScript helps prevent common problems when defining functions, dealing with parameters, and producing results. Table 8-1 summarizes the chapter.
Adam Freeman

Chapter 9. Using Arrays, Tuples, and Enums

Abstract
The examples so far in this part of the book have focused on primitive types, which has let me introduce the basic TypeScript features. In real projects, related data properties are grouped together to create objects. In this chapter, I describe the TypeScript support for simple data structures, starting with arrays. Table 9-1 summarizes the chapter.
Adam Freeman

Chapter 10. Working with Objects

Abstract
In this chapter, I describe the way that TypeScript deals with objects. As explained in Chapters 3 and 4, JavaScript has a fluid and flexible approach to dealing with objects, and TypeScript aims to strike a balance between preventing the most common mistakes while allowing useful features to be preserved. This is a theme that is continued in Chapter 11, where I describe the TypeScript support for using classes. Table 10-1 summarizes the chapter.
Adam Freeman

Chapter 11. Working with Classes and Interfaces

Abstract
In this chapter, I describe the features that TypeScript provides for working with classes and introduce the interface feature, which provides an alternative approach to describing the shape of objects. Table 11-1 summarizes the chapter.
Adam Freeman

Chapter 12. Using Generic Types

Abstract
Generic types are placeholders for types that are resolved when a class or function is used, allowing type-safe code to be written that can deal with a range of different types, such as collection classes. This is a concept that is more easily demonstrated than explained, so I start this chapter with an example of the problem that generic types solve and then describe the basic ways that generic types are used. In Chapter 13, I describe the advanced generic type features that TypeScript provides. Table 12-1 summarizes the chapter.
Adam Freeman

Chapter 13. Advanced Generic Types

Abstract
In this chapter, I continue to describe the generic type features provided by TypeScript and focus on the advanced features. I explain how generic types can be used with collections and iterators, introduce the index types and type mapping features, and describe the most flexible of the generic type features: conditional types. Table 13-1 summarizes the chapter.
Adam Freeman

Chapter 14. Working with JavaScript

Abstract
TypeScript projects generally incorporate some amount of pure JavaScript code, either because the application is written in both TypeScript and JavaScript or because the project relies on third-party JavaScript packages installed using NPM. In this chapter, I describe the features that TypeScript provides for working with JavaScript. Table 14-1 summarizes the chapter.
Adam Freeman

Creating Web Applications

Frontmatter

Chapter 15. Creating a Stand-Alone Web App, Part 1

Abstract
In this part of the book, I show you how TypeScript fits into the development process for the three most popular web application frameworks: Angular, React, and Vue.js. In each case, I go through the process of creating the project, setting up a web service, and writing a simple web application. In this chapter, I create the same web application without using any of these frameworks, providing a baseline for understanding the features they provide and context for how TypeScript features are used.
Adam Freeman

Chapter 16. Creating a Stand-Alone Web App, Part 2

Abstract
In this chapter, I complete the stand-alone web application and prepare it for deployment, demonstrating the way that a TypeScript project dovetails with standard development processes for deployment. For quick reference, Table 16-1 lists the TypeScript compiler options used in this chapter.
Adam Freeman

Chapter 17. Creating an Angular App, Part 1

Abstract
In this chapter, I start the process of creating an Angular web application that has the same set of features as the example in Chapters 15 and 16. Unlike other frameworks, where using TypeScript is an option, Angular puts TypeScript at the heart of web application development and relies on its features, especially decorators. For quick reference, Table 17-1 lists the TypeScript compiler options used in this chapter.
Adam Freeman

Chapter 18. Creating an Angular App, Part 2

Abstract
In this chapter, I continue the development of the Angular web application started in Chapter 17 by adding the remaining features and preparing the application for deployment into a container. For quick reference, Table 18-1 lists the TypeScript compiler options used in this chapter.
Adam Freeman

Chapter 19. Creating a React App

Abstract
In this chapter, I start the process of creating a React application that has the same features as the stand-alone and Angular examples from earlier chapters. TypeScript is optional in React development, but there is good support available, and React development with TypeScript provides a good developer experience. For quick reference, Table 19-1 lists the TypeScript compiler options used in this chapter.
Adam Freeman

Chapter 20. Creating a React App, Part 2

Abstract
In this chapter, I complete the React web application by adding URL routing and the remaining components before preparing the application for deployment in a container. For quick reference, Table 20-1 lists the TypeScript compiler options used in this chapter.
Adam Freeman

Chapter 21. Creating a Vue.js App, Part 1

Abstract
In this chapter, I start the process of building the example web application using Vue.js. Of the three frameworks that I have demonstrated in this part of the book, it is Vue.js that changes the most when TypeScript is used, providing not only type checking but also a completely different way of working with the framework’s core building blocks. For quick reference, Table 21-1 lists the TypeScript compiler options used in this chapter.
Adam Freeman

Chapter 22. Creating a Vue.js App, Part 2

Abstract
In this chapter, I complete the Vue.js web application by implementing URL routing and adding the remaining components, before preparing for deployment in a container. For quick reference, Table 22-1 lists the TypeScript compiler options used in this chapter.
Adam Freeman
Additional information