Preface....11
Dear Readers:....14
1. Introduction....16
2. Typescript Basics....22
2.1. What is Typescript?....22
2.2. Static Typing....26
2.2.1. Type annotations....26
2.2.2. Static type checking....26
2.2.3. Type inference....28
2.3. Notes on Development Process....29
2.4. The Typescript Compiler....31
2.5. Typescript JSON Configuration File....34
2.5.1. Top-level options....34
2.5.2. compilerOptions....36
2.5.3. strict....39
2.5.4. strictNullChecks....39
2.5.5. noImplicitAny....40
2.5.6. noImplicitThis....40
3. Module System....42
3.1. ES Modules....42
3.2. Typescript Modules....43
3.3. Module Exports....43
3.3.1. (Regular) exports....44
3.3.2. Default exports....46
3.4. Module Imports....47
3.4.1. Module import....47
3.4.2. import - from Declaration....48
3.4.3. Default import....48
3.4.4. Name list import....48
3.4.5. Namespace import....49
3.5. Typescript Namespaces....49
4. Variables....50
4.1. The const Declaration....50
4.2. The let Declaration....51
4.3. The var Declaration....53
5. Basic Types....55
5.1. Javascript Types....55
5.1.1. The typeof operator....56
5.1.2. The instanceof operator....57
5.2. Strict Equality....58
5.3. Primitive Types....59
5.3.1. The boolean, number, and string types....59
5.3.2. The bigint Type....60
5.3.3. The symbol type....60
5.4. Literal Types....61
5.5. The any Type....62
5.6. The unknown Type....63
5.7. The null and undefined Types....65
5.8. The never Type....67
5.9. The void Type....68
5.10. The object Type....69
5.11. Function Types....70
5.12. Array Types....71
5.13. Tuple Types....72
5.14. Enum Types....74
6. Type Aliases....76
7. Type Annotations....79
7.1. Variable Annotations....79
7.2. Function Annotations....80
8. Assertions....84
8.1. Type Assertions....84
8.2. Const Assertions....85
8.3. Non-Null Assertions....87
9. Generics....89
9.1. Why Generics?....89
9.2. Generic Functions....93
9.3. Generic Types....95
9.4. Generic Type Constraints....96
10. Arrays....98
10.1. Generic Array....98
10.1.1. Array creation....99
10.1.2. Array iteration....100
10.2. Generic ReadonlyArray....101
11. Algebraic Data Types....103
11.1. Tuples....103
11.1.1. Fixed-size tuples....103
11.1.2. Generic tuples....105
11.1.3. Variadic tuples....105
11.2. Readonly Tuples....108
11.3. Union Types....109
11.3.1. Narrowing....111
11.4. Discriminated Unions....112
11.5. Intersection Types....114
12. Function Types....116
12.1. Function Definitions....116
12.2. Arrow Function Definitions....117
12.3. Function Types....118
12.3.1. Function type expressions....118
12.3.2. Function return types....120
12.3.3. Generic function alias....121
12.3.4. The Function type....121
12.4. Parameter List....122
12.4.1. Parameter initializers....123
12.5. Optional Parameters....123
12.6. The Rest Parameter....125
12.7. Parameter Destructuring....128
12.8. The this Parameter....130
12.8.1. Global context....131
12.8.2. Function context....131
12.9. Typescript Function Overloading....133
12.9.1. Overload signatures vs the implementation signature....133
13. Object Types....136
13.1. Object Literal Types....136
13.1.1. The empty object literal type....138
13.2. Object Type Members....139
13.2.1. Optional properties....139
13.2.2. Readonly properties....140
13.3. Index Signatures....142
13.3.1. Typescript index signatures....143
13.4. Getters and Setters....145
13.5. Member Methods....148
13.5.1. Method overloading....149
13.6. Structural Subtyping....149
14. Interfaces....152
14.1. Interface Types....152
14.2. Extending Interfaces....154
15. Classes....156
15.1. The ECMAScript Class....156
15.1.1. Class declaration....156
15.1.2. Class body....157
15.1.3. Fields....158
15.1.4. Accessors....159
15.1.5. Methods....159
15.1.6. Constructors....159
15.1.7. Static blocks....160
15.1.8. Inheritance....160
15.2. The Typescript Class....160
15.2.1. Constructors....161
15.2.2. Member visibility....162
15.2.3. Parameter properties....163
15.3. Abstract Classes....164
15.4. Implementing Interfaces....166
15.4.1. Implementing multiple interfaces....168
15.5. Generic Classes....169
16. Type Narrowing....171
16.1. Control Flow Analysis....171
16.2. The typeof Type Guard....171
16.3. The instanceof Type Guard....172
16.4. The in Operator Narrowing....174
16.5. Discriminated Unions....175
17. Advanced Types....179
17.1. Template Literal Types....179
17.2. The typeof Type Operator....180
17.3. The keyof Type Operator....181
17.4. Indexed Access Types....182
17.5. Conditional Types....183
17.6. Mapped Types....184
Appendix A: How to Use This Book....186
About the Author....190
About the Series....192
Community Support....194
Become a Better Javascript Programmer!
Typescript is a better Javascript, It is a statically typed, safe programming language, which uses more or less the same syntax as Javascript. Typescript programs are compiled to Javascript, and they can be run anywhere Javascript is supported. It is widely used by professional Javascript developers, for frontend development (e.g., Angular, React, and VueJS) as well as for backend development (e.g., Node.Js and Deno).
This book is a "mini" language reference on the Typescript programming language. We go through all essential features of Typescript (as of versions 4.8 and 4.9), and some new ECMAScript features, in this book.
Although it is written as a reference, however, you can read it more or less from beginning to end and you should be able to get the overall picture of the Typescript language if you have some prior experience with programming in Typescript or modern Javascript (ES2015 and later).