Table of Contents
Preface xi
1 Introduction 1
2 TypeScript: A 10_000 Foot View 5
The Compiler 5
The Type System 7
TypeScript Versus JavaScript 8
Code Editor Setup 11
tsconfig.json 11
tslint.json 13
Index.ts 13
Exercises 15
3 All About Types 17
Talking About Types 18
The ABCs of Types 19
Any 19
Unknown 20
Boolean 21
Number 22
Bigint 23
String 23
Symbol 24
Objects 25
Intermission: Type Aliases, Unions, and Intersections 30
Arrays 33
Tuples 35
Null, undefined, void, and never 37
Enums 39
Summary 43
Exercises 44
4 Functions 45
Declaring and Invoking Functions 45
Optional and Default Parameters 47
Rest Parameters 48
Call, apply, and bind 50
Typing this 50
Generator Functions 52
Iterators 53
Call Signatures 55
Contextual Typing 58
Overloaded Function Types 59
Polymorphism 65
When Are Generics Bound? 69
Where Can You Declare Generics? 70
Generic Type Inference 72
Generic Type Aliases 73
Bounded Polymorphism 75
Generic Type Defaults 79
Type-Driven Development 80
Summary 81
Exercises 82
5 Classes and Interfaces 83
Classes and Inheritance 83
Super 89
Using this as a Return Type 89
Interfaces 91
Declaration Merging 93
Implementations 94
Implementing Interfaces Versus Extending Abstract Classes 96
Classes Are Structurally Typed 97
Classes Declare Both Values and Types 98
Polymorphism 100
Mixins 101
Decorators 104
Simulating final Classes 107
Design Patterns 108
Factory Pattern 108
Builder Pattern 109
Summary 110
Exercises 111
6 Advanced Types 113
Relationships Between Types 114
Subtypes and Supertypes 114
Variance 115
Assignability 121
Type Widening 122
Refinement 126
Totality 130
Advanced Object Types 132
Type Operators for Object Types 132
The Record Type 137
Mapped Types 137
Companion Object Pattern 140
Advanced Function Types 141
Improving Type Inference for Tuples 141
User-Defined Type Guards 142
Conditional Types 143
Distributive Conditionals 144
The infer Keyword 145
Built-in Conditional Types 146
Escape Hatches 147
Type Assertions 148
Nonnull Assertions 149
Definite Assignment Assertions 151
Simulating Nominal Types 152
Safely Extending the Prototype 154
Summary 156
Exercises 157
7 Handling Errors 159
Returning null 160
Throwing Exceptions 161
Returning Exceptions 163
The Option Type 165
Summary 171
Exercises 172
8 Asynchronous Programming, Concurrency, and Parallelism 173
JavaScript's Event Loop 174
Working with Callbacks 176
Regaining Sanity with Promises 178
Async and await 183
Async Streams 184
Event Emitters 184
Typesafe Multithreading 187
In the Browser: With Web Workers 187
In NodeJS: With Child Processes 196
Summary 197
Exercises 198
9 Frontend and Backend Frameworks 199
Frontend Frameworks 199
React 201
Angular 6/7 207
Typesafe APIs 210
Backend Frameworks 212
Summary 213
10 Namespaces.Modules 215
A Brief History of JavaScript Modules 216
Import, export 218
Dynamic Imports 219
Using CommonJS and AMD Code 221
Module Mode Versus Script Mode 222
Namespaces 222
Collisions 225
Compiled Output 225
Declaration Merging 226
Summary 228
Exercise 228
11 Interoperating with JavaScript 229
Type Declarations 230
Ambient Variable Declarations 233
Ambient Type Declarations 234
Ambient Module Declarations 235
Gradually Migrating from JavaScript to TypeScript 236
Step 1 Add TSC 237
Step 2a Enable Typechecking for JavaScript (Optional) 238
Step 2b Add JSDoc Annotations (Optional) 239
Step 3 Rename Your Files to .ts 240
Step 4 Make It strict 241
Type Lookup for JavaScript 242
Using Third-Party JavaScript 244
JavaScript That Comes with Type Declarations 245
JavaScript That Has Type Declarations on DefinitelyTyped 245
JavaScript That Doesn't Have Type Declarations on DennitelyTyped 246
Summary 247
12 Building and Running TypeScript 249
Building Your TypeScript Project 249
Project Layout 249
Artifacts 250
Dialing In Your Compile Target 251
Enabling Source Maps 255
Project References 255
Error Monitoring 258
Running TypeScript on the Server 258
Running TypeScript in the Browser 259
Publishing Your TypeScript Code to NPM 261
Triple-Slash Directives 262
The types Directive 262
The amd-module Directive 264
Summary 265
13 Conclusion 267
A Type Operators 269
B Type Utilities 271
C Scoped Declarations 273
D Recipes for Writing Declaration Files for Third-Party JavaScript Modules 275
E Triple-Slash Directives 283
F TSC Compiler Hags for Safety 285
G TSX 287
Index 291