Book Image

Learning TypeScript

By : Remo H. Jansen
Book Image

Learning TypeScript

By: Remo H. Jansen

Overview of this book

<p>TypeScript is an open source and cross-platform typed superset of JavaScript that compiles to plain JavaScript that runs in any browser or any host. It allows developers to use the future versions of JavaScript (ECMAScript 6 and 7) today. TypeScript adds optional static types, classes, and modules to JavaScript, to enable great tooling and better structuring of large JavaScript applications.</p> <p>This book is a step-by-step guide that will get you started with TypeScript with the help of practical examples. You start off by understanding the basics of TypeScript. Next, automation tools like Grunt are explained followed by a detailed description of function, generics, callbacks and promises. After this, object-oriented features and the memory management functionality of TypeScript are explained. At the end of this book, you will have learned enough to implement all the concepts and build a single page application from scratch.</p>
Table of Contents (18 chapters)
Learning TypeScript
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Over the past few years, the JavaScript code base of an average web application has been exponentially growing. However, the current JavaScript specification (ECMAScript 5 or ES5) was designed several years ago and lacks some features that are necessary to cope with the complexity that we can find in large-scale JavaScript applications today. As a result of these missing features, maintainability problems have arisen.

The upcoming JavaScript version (ECMAScript 6 or ES6) is meant to solve some of the maintainability issues of JavaScript, but its implementation is in progress and many incompatible web browsers are still in use today. For these reasons, the wide adoption of the ES6 specification is expected to be a slow process.

Microsoft spent two years developing TypeScript with the goal of resolving the maintainability and scalability problems of JavaScript and publicly announced it in October 2012:

 

"We designed TypeScript to meet the needs of the JavaScript programming teams that build and maintain large JavaScript programs. TypeScript helps programming teams to define interfaces between software components and to gain insight into the behavior of existing JavaScript libraries. TypeScript also enables teams to reduce naming conflicts by organizing their code into dynamically loadable modules. Typescript's optional type system enables JavaScript programmers to use highly-productive development tools and practices: static checking, symbol-based navigation, statement completion, and code re-factoring."

 
 --TypeScript Language Specification 1.0

Some developers with years of experience in the field find it challenging to define what is a large-scale JavaScript application. When referring to this term, we will avoid taking into account the number of lines of code in the application. It is much better to consider the number of modules, dependencies between modules as measures of the application's scale. We will define large-scale applications as non-trivial applications that require significant developer effort to be maintained.

Learning TypeScript introduces many of the TypeScript features in a simple and easy-to-understand format. This book will teach you everything you need to know in order to implement large-scale JavaScript applications using TypeScript. Not only does it teach TypeScript's core features, which are essential to implement a web application, but it also explores the power of some tools, design principles, best practices, and it also demonstrates how to apply them in a real-life application.

What this book covers

Chapter 1, Introducing TypeScript, introduces the core features of TypeScript, including the optional static type notation system, operators, functions, interfaces, and modules. The chapter also demonstrates how to apply these features in a real-life example.

Chapter 2, Automating Your Development Workflow, introduces some automation tools, such as Gulp and Karma, to maximize your productivity as a developer. This chapter also introduces some tools that facilitate the usage of third-party libraries in the development of TypeScript applications.

Chapter 3, Working with Functions, provides an in-depth look at the functions in TypeScript. This chapter also teaches you everything you need to know about asynchronous programming in order to become a proficient TypeScript developer.

Chapter 4, Object-Oriented Programming with TypeScript, provides an in-depth look at object-oriented programming in TypeScript, including classes, interfaces, and modules and encourages adherence to good practices (SOLID principles). It also covers features such as inheritance, mixings, and generics, which facilitate the reusability of our code.

Chapter 5, Runtime, helps you understand how the runtime works. Understanding how the runtime works can help us identify potential performance issues and allow us to be much more effective as TypeScript developers.

Chapter 6, Application Performance, provides the necessary knowledge to make effective use of the available resources of a system. This chapter teaches you how to test the performance of a TypeScript web application and how to automate some common tasks in the performance optimization process of a TypeScript application.

Chapter 7, Application Testing, demonstrates how to use the behavior-driven development methodology together with the leading TypeScript testing tools to create bug-free applications. In this chapter, you will learn how to write TypeScript unit tests with Karma, Mocha, Chai, and Sinon.JS, how to create end-to-end tests using Nightwatch.js, and how to generate test coverage reports using Istanbul.

Chapter 8, Decorators, provides an in-depth look at decorators, including class, property, parameter, and method decorators. This chapter also includes an introduction to the reflection metadata API.

Chapter 9, Application Architecture, demonstrates some of the core architecture principles of a modern web application. The chapter introduces the concept of the single-page web application and its common components and features (models, views, controllers, routers, templates, and so on). This chapter will teach you everything you need to know in order to understand the majority of single-page web application frameworks by implementing a real-life single-page web application framework from scratch.

Chapter 10, Putting Everything Together, demonstrates how to apply the majority of the concepts exposed in this book by implementing a real-life single-page web application using TypeScript.

What you need for this book

The examples in this book are written using TypeScript 1.5. You will need the TypeScript compiler and a text editor. This book explains how to use Atom, but it is also possible to use other editors, such as Visual Studio 2015, Visual Studio Code, or Sublime Text.

You also need an Internet connection to download the required references and online packages and libraries, such as jQuery, Mocha, and Gulp. Depending on your operating system, you will need a user account with administrative privileges in order to install some of the tools used in this book.

Chapter 2, Automating Your Development Workflow, describes the setup of a development environment.

Who this book is for

If you are an intermediate-level JavaScript developer aiming to learn TypeScript to build beautiful web applications, then this book is for you. No prior knowledge of TypeScript is required but a basic understanding of jQuery is expected.

The book introduces TypeScript from basic to advanced language constructs and object-oriented techniques for getting the most out of the TypeScript language and compiler. This book will show you how to incorporate strong typing, object-oriented principles, design patterns, and the best practices for managing the complexity of large-scale JavaScript applications with ease.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

class Greeter {
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

function MathHelper() { /* ... */ }

// class method
MathHelper.areaOfCircle = function(radius) {
  return radius * radius * this.PI; 
}

// class property
MathHelper.PI = 3.14159265359;

Any command-line input or output is written as follows:

git clone https://github.com/user-name/repository-name.git

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "In this tab, we can select Create JavaScript CPU Profile and then click on the Start button to start recording the CPU usage."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.