Cover Page....1
Title Page....2
Copyright Page....3
Dedication Page....4
About the Author....5
Technical Reviewers....6
Acknowledgements....7
Preface....8
Errata....11
Table of Contents....14
1. Introduction to TypeScript and its Benefits....24
Introduction....24
Structure....24
Introduction to TypeScript....24
A short history of TypeScript....25
Main components of TypeScript....25
TypeScript’s type system....26
Advantages of using TypeScript....27
Catching bugs....27
Readability....28
Refactoring....29
Future language features....29
Disadvantages of TypeScript....30
The ways in which TypeScript prevents bugs....31
Steps to begin using TypeScript....32
Type-driven development....35
Conclusion....37
References....37
2. Setting up a Development Environment....39
Introduction....39
Structure....39
Installing dependencies....40
Version numbers....40
Installing Node.js On Windows....40
Installing Node.js on Mac....41
Installing a code editor....42
Installing TypeScript globally....42
Creating a new TypeScript project....43
The tsconfig.json file....44
Installing TypeScript locally to a project....45
Configuring TypeScript with tsconfig.json....46
Default enabled configuration options....47
target....47
module....48
esModuleInterop....49
forceConsistentCasingInFileNames....49
strict....49
skipLibCheck....51
Commonly used configuration options....51
files....51
include....52
exclude....52
baseUrl....52
rootDir....52
paths....53
outDir....53
resolveJsonModule....54
Top-level configuration options....54
Updating the project configuration....54
Enabling TypeScript checking in JavaScript....55
Default behavior....56
Enabling type checking....57
Adding JSDoc annotations....58
Example project structure and use....61
Conclusion....61
References....62
3. Basic Type Annotations....63
Introduction....63
Structure....63
Primitive types....64
BigInt....64
Boolean....66
Number....66
Null....67
String....68
Symbol....68
Undefined....69
The any type....70
The unknown type....72
The never type....73
The as operator....74
Down-casting....74
Compound casting....75
Older type-casting syntax....77
Union types....78
Literal types....79
Literal union types....80
Type aliases....81
Type assertion....83
Non-null assertion operator....85
Conclusion....85
References....87
4. Using the TypeScript Compiler....88
Introduction....88
Structure....88
Compiling our TypeScript files....89
Inspecting compiled files....94
CLI flags....95
--version....96
--listFilesOnly....98
--showConfig....100
--help....102
Using watch mode....103
watchFile....105
watchDirectory....106
fallbackPolling....106
synchronousWatchDirectory....107
excludeDirectories....107
excludeFiles....108
assumeChangesOnlyAffectDirectDependencies....108
Environment variables....108
Building projects....109
Build-specific flags....113
Integrating with other build tools....115
Integrating with webpack....115
TypeScript webpack configuration....116
Using third-party libraries....119
Generating .d.ts files....121
Generating d.ts files from .js files....123
Conclusion....126
References....127
5. Enums, Interfaces, and Namespaces....128
Introduction....128
Structure....128
Interfaces....129
Interface merging....134
Extending interfaces....135
Namespaces....138
Namespace merging....140
Enums....141
Numeric enums....142
Reverse mapping....146
Exhaustiveness and the never type....146
String enums....149
Heterogeneous enums....151
Computed and constant enums....153
Literal enums....154
Inlining enums....156
Using the keyof operator....158
Conclusion....159
References....159
6. Objects, Arrays, and Tuples in TypeScript....160
Introduction....160
Structure....160
Arrays....161
Array type inference....162
Read-only arrays....164
Tuples....165
Optional elements in tuples....168
Rest elements in tuples....168
Read-only tuples....169
Object types....169
Property modifiers....174
Index signatures....175
Intersections....176
Generic object types....178
Readonly utility type....180
Conclusion....181
References....182
7. Functions in TypeScript....183
Introduction....183
Structure....183
Parameter Type and Return Type Annotations....184
Type Inference for Functions....186
Arrow Functions....187
Type Inference for Arrow Functions....189
Optional Parameters....189
Rest Parameters....190
Rest Arguments....191
Destructured Parameters....192
Void return type....195
Function Type Expressions....197
Call signatures....198
Function Type Interfaces....200
This Parameter....201
Function overloads....203
Overloading Arrow Functions....205
Generator functions....207
Generic functions....213
Generic Function Constraints....214
Conclusion....216
References....216
8. Classes in TypeScript....218
Introduction....218
Structure....218
Class Declarations....219
Class Expressions....220
Constructors....220
Constructor Overloading....223
Parameter Properties....224
Access Modifiers....225
Private Members in JavaScript....227
Getters and Setters....228
This Parameter....230
Index Signatures....232
Implementing an Interface....233
Static Class Members....234
Static Blocks....235
Inheritance....237
Abstract Classes....240
Abstract Properties....241
Abstract Methods....242
Generic Classes....243
Decorators....245
TypeScript Design Patterns....246
Conclusion....249
References....249
9. Control Flow Analysis....250
Introduction....250
Structure....250
Narrowing....251
Widening....252
Type Guards....253
Truthiness Type Guards....254
Narrowing with Typeof....255
Handling null Values....256
Narrowing with Instanceof....257
Narrowing with the in Operator....259
Narrowing with Type Predicates....260
Discriminated Unions....263
Assertion Functions....264
Using as const....268
Conclusion....272
References....272
10. Manipulating Types....273
Introduction....273
Structure....273
Generics....273
Generic Interfaces....274
Generic Types....274
Generic Classes....275
Generic Functions....275
Conditional Types....277
Indexed Access Types....278
Mapped Types....279
Adding and Removing Property Modifiers....280
Remapping Property Keys....282
Template Literal Types....283
Capitalize....285
Uncapitalize....286
Uppercase....286
Utility Types....287
Awaited....287
ConstructorParameters....288
Exclude....290
Extract....291
InstanceType....292
NonNullable....294
Omit....296
OmitThisParameter....297
Partial....299
Parameters....301
Pick....302
Readonly....303
Record....305
Required....306
ReturnType....308
ThisParameterType....309
ThisType....310
Conclusion....311
References....312
11. TypeScript Modules....313
Introduction....313
Structure....313
Modules in TypeScript....313
Importing and exporting modules....314
Type-only imports and exports....316
Compiled modules....316
Module-related configuration options....318
Module....319
Module resolution....321
Base URL....322
Paths....322
Rootdirs....323
Type roots....324
Module suffixes....324
Resolve JSON module....325
Module resolution....326
Compiler directives....328
Reference path....329
Reference types....330
Reference lib....331
No default lib....331
AMD module....331
AMD dependency....331
Barrel files....332
Nested barrels....334
Augmenting modules....335
Conclusion....336
References....336
12. Creating Declaration Files....338
Introduction....338
Structure....338
Creating declaration files....339
Declaring global libraries....339
Enhancing Intellisense with JSDoc....342
Declaring global functions and variables....344
Augmenting built-ins....345
Declaring modular libraries....346
Declaring default exports....348
Declaring classes....350
Declaring CommonJS modules....351
Declaring UMD modules....352
Publishing declarations....355
Publishing with the library....355
Publishing to Definitely Typed....356
Testing types....360
Conclusion....362
References....362
13. Building a Conference App with Angular and TypeScript....364
Introduction....364
Structure....364
Getting started....365
Running build tasks....369
Unit tests....369
Linting....371
Serving the application....374
Creating the application shell....376
Creating a data model....379
Adding views....382
Home view....382
Adding routing....388
Building the add-conference view....390
Adding the conferences view....400
Changing the default locale....408
Adding a page not found component....411
Handling data....414
Unit testing....425
Continuing with the example application....428
Conclusion....429
References....429
Index....431
This book provides a comprehensive guide to TypeScript, a programming language that extends JavaScript with powerful features like static typing, classes, and interfaces. The book is divided into thirteen chapters that cover everything from setting up a development environment to building an Angular app with TypeScript.We start with an introduction to TypeScript and its benefits and go on to explain how TypeScript can help developers write more maintainable, scalable code and catch errors before they make it to production. The book then dives into more technical topics like basic type annotations, using the TypeScript compiler, advanced features like enums and interfaces, and manipulating types.The book then discusses chapters on classes and control flow analysis that are particularly helpful for developers looking to build more complex applications. The book teaches by example, with numerous code examples that illustrate the main concepts and approaches towards writing TypeScript programs and codes, and finishes with a comprehensive practical example that shows step-by-step development of a modern web application using TypeScript and Angular.
This book is targeted towards front-end developers who are familiar with JavaScript and are interested in expanding their skillset with TypeScript. Whether you are a beginner with little to no experience or an intermediate developer looking for a reference guide, this book is designed to help you supercharge your development and enhance your tooling. With its clear and concise explanations, it's perfect for those who are new to TypeScript and looking to gain a deeper understanding of its capabilities.