[TYPESCRIPT][Pro TypeScript Application-Scale JavaScript Development].pdf

(3867 KB) Pobierz
www.it-ebooks.info
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
www.it-ebooks.info
Contents at a Glance
About the Author �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
xv
Acknowledgments �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
xvii
Introduction �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
xix
Chapter 1: TypeScript Language Features �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½1
Chapter 2: The Type System �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½47
Chapter 3: Object Orientation in TypeScript �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½63
Chapter 4: Understanding the Runtime �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½87
Chapter 5: Running TypeScript in a Browser �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½107
Chapter 6: Running TypeScript on a Server �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½141
Chapter 7: Exceptions, Memory, and Performance�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½163
Chapter 8: Using JavaScript Libraries �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½177
Chapter 9: Automated Testing �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½185
Appendix 1: JavaScript Quick Reference �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½197
Appendix 2: TypeScript Compiler �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½203
Appendix 3: Bitwise Flags �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½205
Appendix 4: Coding Katas�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½209
Index �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½213
v
www.it-ebooks.info
Introduction
Atwood’s Law: any application that can be written in JavaScript will eventually be written in
JavaScript.
—Jeff Atwood
TypeScript is a language created by Microsoft and released under an open-source Apache 2.0 License (2004). The
language is focused on making the development of JavaScript programs scale to many thousands of lines of code.
The language attacks the large-scale JavaScript programming problem by offering better design-time tooling,
compile-time checking, and dynamic module loading at runtime.
As you might expect from a language created by Microsoft, there is excellent support for TypeScript within
Visual Studio, but other development tools have also added support for the language, including WebStorm, Eclipse,
Sublime Text, Vi, IntelliJ, and Emacs among others. The widespread support from these tools as well as the permissive
open-source license makes TypeScript a viable option outside of the traditional Microsoft ecosystem.
The TypeScript language is a typed superset of JavaScript, which is compiled to plain JavaScript. This makes
programs written in TypeScript highly portable as they can run on almost any machine—in web browsers, on web
servers, and even in native applications on operating systems that expose a JavaScript API, such as WinJS on
Windows 8 or the Web APIs on Firefox OS.
The language features found in TypeScript can be divided into three categories based on their relationship
to JavaScript (see Figure 1). The first two sets are related to versions of the ECMA-262 ECMAScript Language
Specification, which is the official specification for JavaScript. The ECMAScript 5 specification forms the basis of
TypeScript and supplies the largest number of features in the language. The ECMAScript 6 specification adds modules
for code organization and class-based object orientation, and TypeScript has included these since its release in
October 2012. The third and final set of language features includes items that are not planned to become part of the
ECMAScript standard, such as generics and type annotations. All of the TypeScript features can be converted into
valid ECMAScript 5 and most of the features can be converted into the ancient ECMAScript 3 standard if required.
Figure 1.
TypeScript language feature sources
Because TypeScript is such a close relative of JavaScript, you can consume the myriad existing libraries and
frameworks written in JavaScript. Angular, Backbone, Bootstrap, Durandal, jQuery, Knockout, Modernizr, PhoneGap,
Prototype, Raphael, Underscore, and many more are all usable in TypeScript programs. Correspondingly, once your
TypeScript program has been compiled it can be consumed from any JavaScript code.
xix
www.it-ebooks.info
IntroduCtIon
TypeScript’s similarity to JavaScript is beneficial if you already have experience with JavaScript or other C-like
languages. The similarity also aids the debugging process as the generated JavaScript correlates closely to the original
TypeScript code.
If you still need to be convinced about using TypeScript or need help convincing others, I summarize the benefits
of the language as well as the problems it can solve in the following. I also include an introduction to the components
of TypeScript and some of the alternatives. If you would rather get started with the language straight away, you can
skip straight to Chapter 1.
Who This Book Is For
This book is for programmers and architects working on large-scale JavaScript applications, either running in a
browser, on a server, or on an operating system that exposes a JavaScript API. Previous experience with JavaScript or
another C-like language is useful when reading this book, as well as a working knowledge of object orientation and
design patterns.
Structure
This book is organized into nine chapters and four appendices.
Chapter 1:
TypeScript Language Features: describes the language features in detail, from
simple type annotations to important structural elements, with stand-alone examples of
how to use each one.
Chapter 2:
The Type System: explains the details of working within TypeScript’s structural
type system and describes the details on type erasure, type inference, and ambient
declarations.
Chapter 3:
Object Orientation in TypeScript: introduces the important elements of object
orientation and contains examples of design patterns and SOLID principles in TypeScript.
This chapter also introduces the concept of mixins with practical examples.
Chapter 4:
Understanding the Runtime: describes the impact of scope, callbacks, events,
and extensions on your program.
Chapter 5:
Running TypeScript in a Browser: a thorough walk-through including
working with the Document Object Model, AJAX, session and local storage, IndexedDB,
geolocation, hardware sensors, and web workers as well as information on packaging your
program for the web.
Chapter 6:
Running TypeScript on a Server: an explanation of running programs on a
JavaScript server with examples for Node and a basic end-to-end application example
written in Express and Mongoose.
Chapter 7:
Exceptions, Memory, and Performance: describes exceptions and exception
handling with information on memory management and garbage collection. Includes a
simple performance testing utility to exercise and measure your program.
Chapter 8:
Using JavaScript Libraries: explains how to consume any of the millions of
JavaScript libraries from within your TypeScript program, including information on how to
create your own type definitions and convert your JavaScript program to TypeScript.
Chapter 9:
Automated Testing: a walk-through of automated testing in your TypeScript
program with examples written using the Jasmine framework.
xx
www.it-ebooks.info
Zgłoś jeśli naruszono regulamin