Book Image

Mastering TypeScript

By : Nathan Rozentals
Book Image

Mastering TypeScript

By: Nathan Rozentals

Overview of this book

<p>The TypeScript compiler and language has brought JavaScript development up to the enterprise level, yet still maintains backward compatibility with existing JavaScript browsers and libraries.</p> <p>Packed with practical code samples, this book brings the benefits of strongly typed, object-oriented programming and design principles into the JavaScript development space. Starting with core language features, and working through more advanced topics such as generics and modules, you will learn how to gain maximum benefit from your JavaScript development with TypeScript. With a strong focus on test-driven development and coverage of many popular JavaScript frameworks, you can fast-track your TypeScript knowledge to a professional level. By the end of this book, you will be able to confidently implement a TypeScript application from scratch.</p>
Table of Contents (17 chapters)
Mastering TypeScript
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
TypeScript – Tools and Framework Options
2
Types, Variables and Function Techniques
Index

Preface

The TypeScript language and compiler has been a huge success story since its release in late 2012. It has quickly carved out a solid footprint in the JavaScript development community, and continues to go from strength to strength. Many large-scale JavaScript projects, including projects by Adobe, Mozilla, and Asana, have made the decision to switch their code base from JavaScript to TypeScript. Recently, the Microsoft and Google teams announced that Angular 2.0 will be developed with TypeScript, thereby merging the AtScript and TypeScript languages into one.

This large-scale industry adoption of TypeScript shows the value of the language, the flexibility of the compiler, and the productivity gains that can be realized with its rich development toolset. On top of this industry support, the ECMAScript 6 standard is getting closer and closer to publication, and TypeScript provides a way to use features of this standard in our applications today.

Writing JavaScript single page applications in TypeScript has been made even more appealing with the large collection of declaration files that have been built by the TypeScript community. These declaration files seamlessly integrate a large range of existing JavaScript frameworks into the TypeScript development environment, bringing with it increased productivity, early error detection, and advanced IntelliSense features.

This book is a guide for both experienced TypeScript developers, as well as those who are just beginning their TypeScript journey. With a focus on Test Driven Development, detailed information on integration with many popular JavaScript libraries, and an in-depth look at TypeScript's features, this book will help you with your exploration of the next step in JavaScript development.

What this book covers

Chapter 1, TypeScript – Tools and Framework Options, sets the scene for beginning TypeScript development, by firstly looking at the various benefits of using TypeScript, and then discussing how to set up a development environment.

Chapter 2, Types, Variables and Function Techniques, introduces the reader to the TypeScript language, starting with basic types and type inferences, and then moving on to discusses variables and functions.

Chapter 3, Interfaces, Classes and Generics, builds on the work from the previous chapter, and introduces the object-oriented concepts of interfaces, classes, and inheritance. It then introduces the reader to the syntax and usage of generics within TypeScript.

Chapter 4, Writing and Using Declaration Files, 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. This syntax is designed to be a quick reference guide to declaration file syntax, or a cheat sheet.

Chapter 5, 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 the reader how to write TypeScript that is compatible with three popular JavaScript frameworks—Backbone, Angular, and ExtJs.

Chapter 6, 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, including data-driven and asynchronous tests. The chapter finishes with a discussion on integration testing, test reporting, and using continuous integration build servers.

Chapter 7, Modularization, looks at the two types of module generation that the TypeScript compiler uses: CommonJS and AMD. This chapter shows the reader how to build a CommonJS module for use with Node, and then discusses building AMD modules with Require, Backbone, AMD plugins, and jQuery plugins.

Chapter 8, Object-oriented Programming with TypeScript, discusses advanced object-oriented design patterns, including the Service Location Design Pattern, Dependency Injection, and the Domain Events Design Pattern. The reader is taken through the concepts and ideas of each pattern, and then shown how one might implement these patterns using TypeScript.

Chapter 9, Let's Get Our Hands Dirty, builds a single-page application using TypeScript and Marionette from the ground up. This chapter starts with a discussion on page layout and transition, using an HTML-only version of the application. It then moves on to discuss, build and test the underlying data models and Marionette views that will be used within the application. Finally, the State and Mediator Design Pattern is implemented to manage page transitions and graphical elements.

What you need for this book

You will need the TypeScript compiler and an editor of some sort. The TypeScript compiler is available as a Node.js plugin or a Windows executable; therefore, it will run on any operating system. Chapter 1, TypeScript – Tools and Framework Options describes the setup of a development environment.

Who this book is for

Whether you are a JavaScript developer wanting to learn TypeScript, or an experienced TypeScript developer wanting to take your skills to the next level, this book is for you. From basic to advanced language constructs, Test Driven Development, and object-oriented techniques, you will learn how to get the most out of the TypeScript language and compiler. This book will show you how to incorporate strong typing, object-orientation, and design best practices into your JavaScript applications.

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: "This GruntFile.js is necessary to setup all of the Grunt tasks."

A block of code is set as follows:

class MyClass {
    add(x, y) {
        return x + y;
    }
}

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

class MyClass {
    add(x, y) {
        return x + y;
    }
}

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

tsc app.ts

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: "After selecting a Name and browsing for a directory, clicking on OK will generate a TypeScript project."

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.

Downloading the color images of this book

We also provide you a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: https://www.packtpub.com/sites/default/files/downloads/B03967_9665OS_Graphics.pdf.

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.