Book Image

Mastering TypeScript 3 - Third Edition

By : Nathan Rozentals
Book Image

Mastering TypeScript 3 - Third Edition

By: Nathan Rozentals

Overview of this book

TypeScript is both a language and a set of tools to generate JavaScript. It was designed by Anders Hejlsberg at Microsoft to help developers write enterprise-scale JavaScript. Starting with an introduction to the TypeScript language, before moving on to basic concepts, each section builds on previous knowledge in an incremental and easy-to-understand way. Advanced and powerful language features are all covered, including asynchronous programming techniques, decorators, and generics. This book explores many modern JavaScript and TypeScript frameworks side by side in order for the reader to learn their respective strengths and weaknesses. It will also thoroughly explore unit and integration testing for each framework. Best-of-breed applications utilize well-known design patterns in order to be scalable, maintainable, and testable. This book explores some of these object-oriented techniques and patterns, and shows real-world implementations. By the end of the book, you will have built a comprehensive, end-to-end web application to show how TypeScript language features, design patterns, and industry best practices can be brought together in a real-world scenario.
Table of Contents (16 chapters)
Free Chapter
TypeScript Tools and Framework Options

What this book covers

Chapter 1, TypeScript Tools and Framework Options, sets the scene for beginning TypeScript development. It discusses the benefits of using TypeScript as a language and compiler, and then works through setting up a complete development environment using a number of popular IDEs.

Chapter 2, Types, Variables, and Function Techniques, introduces the reader to the TypeScript language, starting with basic types and type annotations, and then moves on to discuss variables, functions, and advanced language features.

Chapter 3, Interfaces, Classes, and Inheritance, builds on the work from the previous chapter, and introduces the object-oriented concepts and capabilities of interfaces, classes, and inheritance. It then shows these concepts at work through the Factory Design Pattern.

Chapter 4, Decorators, Generics, and Asynchronous Features, discusses the more advanced language features of decorators and generics, before working through the concepts of asynchronous programming. It shows how the TypeScript language supports these asynchronous features through promises and the use of async await constructs.

Chapter 5, Declaration Files and Strict Compiler Options, walks the reader through building a declaration file for an existing body of JavaScript code, and then lists some of the most common syntax used when writing declaration files, as a cheat sheet. It then discusses the strict compiler settings that are available for the compiler—where they should be used, and what benefits they bring.

Chapter 6, Third-Party Libraries, shows the reader how to use declaration files from the DefinitelyTyped repository within the development environment. It then moves on to show how to write TypeScript code that is compatible with three popular JavaScript frameworks—Backbone, AngularJS (version 1), and ExtJS.

Chapter 7, TypeScript Compatible Frameworks, takes a look at popular frameworks that have full TypeScript language integration. It explores the MVC paradigm, and then compares how this design pattern is implemented in Backbone, Aurelia, Angular 2, and React. A sample program that uses form-based input is implemented in each of these frameworks.

Chapter 8, Test-Driven Development, starts with a discussion on what test-driven development is, and then guides the reader through the process of creating various types of unit tests. Using the Jasmine library, it shows how to use data-driven tests, and how to test asynchronous logic. The chapter finishes with a discussion on test runners, test reporting, and using continuous integration build servers.

Chapter 9, Testing TypeScript Compatible Frameworks, shows how to test the sample application built with each of the TypeScript compatible frameworks. It breaks down the testing strategy into Model tests, View tests, and Controller tests, and shows the differences between the testing strategies of these frameworks.

Chapter 10, Modularization, explores what modules are, how they can be used, and the two types of module generation that the TypeScript compiler supports: CommonJS and AMD. It then shows how modules can be used with module loaders, including Require and SystemJS. This chapter then takes an in-depth look at using modules within Node in order to build a sample Express application. Finally, it discusses the use of modules in a serverless environment using AWS Lambda functions.

Chapter 11, Object-Oriented Programming, discusses the concepts of object-oriented programming, and then shows how to arrange application components to conform to object-oriented principles. It then takes an in-depth look at implementing object-oriented best practices by showing how the State and Mediator design patterns can be used to manage complex UI interactions.

Chapter 12, Dependency Injection, discusses the concepts of service location and dependency injection, and how they can be used to solve common application design problems. It then shows how to implement a simple dependency injection framework using decorators.

Chapter 13, Building Applications, explores the fundamental building blocks of web application development, showing how to integrate an Express server and an Angular site. It then explores the all important authorization mechanisms that any site must have in place, with an in-depth discussion of JWT tokens. Finally, this chapter shows how to integrate social medial logins, such as Google or Facebook, into a site.

Chapter 14, Let's Get Our Hands Dirty, builds a single-page application using Angular and Express by combining all of the concepts and components built throughout the book into a single application. These concepts include test-driven development, the State and Mediator pattern, designing and using Express REST endpoints, object-oriented design principles, and modularization. This chapter also explores common techniques when using observables to handle most types of REST API interaction.