Explore the features of this innovative open source language in depth, from working with the type system through object-orientation to understanding the runtime and the TypeScript compiler. This fully revised and updated second edition of Steve Fenton’s popular book covers everything you need to discover this fascinating language and transform your experience of JavaScript development. **What’s New in This Edition** * Coverage of major changes to modules, namespaces, and module loading * New guidance on how to use inference to reduce the effort of using TypeScript * Recommendations on compiler options * A wide range of feature updates from intersections and tuples to async/await and the new approach to mixins **What You’ll Learn** * Understand the TypeScript type system, and how to use it effectively * Apply object-oriented design using TypeScript * Use modules effectively to manage large programs * Integrate existing frameworks and libraries into your TypeScript program **Who This Book Is For** Web developers looking for a modern approach to JavaScript development Contents 5 About the Author 14 Acknowledgments 15 Introduction 16 Chapter 1: TypeScript Language Features 25 JavaScript Is Valid TypeScript 26 Variables 28 Constants 30 Types 30 Type Annotations 31 Primitive Types 33 Object and Dynamic Types 34 Enumerations 34 Bit Flags 35 Constant Enumerations 36 Union Types 37 Literal Types 38 Intersection Types 39 Arrays 39 Tuple Types 41 Dictionary Types 42 Mapped Types 43 Type Assertions 44 Type Guards 45 Discriminated Unions 47 Operators 48 Increment and Decrement 48 Binary Operators 49 Bitwise Operators 50 Logical Operators 50 NOT Operator 51 AND Operator 52 OR Operator 52 Short-Circuit Evaluation 53 Conditional Operator 54 Type Operators 54 Destructuring 54 Spread Operator 58 Functions 59 Optional Parameters 60 Default Parameters 61 Rest Parameters 62 Overloads 62 Specialized Overload Signatures 63 Arrow Functions 65 Function Currying 66 Interfaces 68 Classes 71 Constructors 71 Access Modifiers 73 Properties and Methods 73 Class Heritage 75 Abstract Classes 77 Scope 78 Property and Arrow Function 78 Function Wrapping at Point of Call 79 ECMAScript 5 Bind Function 79 Event Capturing 79 Choosing a Solution 80 Type Information 80 Generics 82 Generic Functions 82 Generic Interfaces 83 Generic Classes 84 Type Constraints 85 TypeScript Futures 85 Summary 86 Key Points 86 Chapter 2: Code Organization 87 Namespaces 88 Modules 92 Module Re-Exporting 94 Default Exports 94 Exports Object 95 Module Loading 95 Dynamic Module Loading 96 Mixing Namespace and Modules 97 Packaging 98 Decorators 100 Configurable Decorators 102 Class Decorators 103 Property Decorators 104 Summary 105 Key Points 106 Chapter 3: The Type System 107 Type Systems 107 Optional Static Types 108 Structural Typing 110 Type Erasure 112 Type Inference 113 Best Common Type 114 Contextual Types 115 Widened Types 115 When to Annotate 115 Duplicate Identifiers 116 Type Checking 117 Ambient Declarations 119 Declaration Files 120 Definitely Typed 121 Summary 121 Key Points 121 Chapter 4: Object Orientation in TypeScript 122 Object Orientation in TypeScript 123 Open Recursion 123 Encapsulation 125 Delegation 127 Polymorphism 129 SOLID Principles 131 The Single Responsibility Principle (SRP) 132 The Open–Closed Principle (OCP) 133 The Liskov Substitution Principle (LSP) 134 The Interface Segregation Principle (ISP) 135 The Dependency Inversion Principle (DIP) 137 Design Patterns 138 The Strategy Pattern 139 The Abstract Factory Pattern 139 Practical Example 140 Mixins 144 TypeScript Mixins 144 When to Use Mixins 146 Restrictions 146 Real Mixins 148 Summary 149 Key Points 150 Chapter 5: Understanding the Runtime 151 Runtime Features 151 Scope 153 Callbacks 156 Passing Functions as Arguments 159 Promises 160 Simple Callbacks 160 Callbacks and Error Handling 162 Promises 165 Multiple Promises 167 Fastest Promise 168 Events 169 TypeScript’s Custom-Event Mechanism 171 Event Phases 173 Extending Objects 173 Extending the Prototype 174 Sealing Objects 176 Alternatives to Extending 177 Summary 178 Key Points 179 Chapter 6: Running TypeScript in a Browser 180 The Anatomy of a Web Browser 180 Reflows and Frames Per Second 181 The Interesting Components 183 The JavaScript Interpreter 183 A Brief History of Networking 184 Storing Data on the Client 185 The Document Object Model 185 Finding Elements 186 Changing Elements 187 Events 188 Frameworks and Libraries 190 Network 191 AJAX 191 HTTP Get 191 HTTP Post 192 WebSockets 194 Real-Time Communications 194 Storage 195 Session Storage 195 Local Storage 196 Storage Restrictions 197 IndexedDB 197 Upgrade Required 198 Querying the Database 199 Adding a New Record 201 Deleting a Record 202 IDBRequest Interface 203 Storage Roundup 204 Geolocation 204 Sensors 206 Battery Status 206 Proximity Sensor 208 Light Sensor 209 Motion and Orientation 209 Other Device Sensors 210 Sensor Roundup 210 Web Workers 211 Packaging Your Program 213 Summary 213 Key Points 213 Chapter 7: Running TypeScript on a Server 215 Install Node 215 Creating a New Project 216 NPM 216 Simple Node Program 219 Request Information 220 Using Express to Write Applications 221 Simple Express Program 222 Multiple Routes 223 Handling Errors 223 Express Book Project 225 Adding the Book Route 228 Collecting Data 229 Installing Mongoose 231 Storing Data 232 Summary 236 Key Points 236 Chapter 8: Exceptions, Memory, and Performance 237 Exceptions 237 Throwing Exceptions 238 Exception Handling 239 Memory 244 Releasing Resources 244 Garbage Collection 245 Performance 248 Summary 252 Key Points 252 Chapter 9: Using JavaScript Libraries 253 Creating Type Definitions 254 Creating a TypeScript Application with Knockout 255 Silencing the Compiler 257 Iteratively Improving Type Definitions 258 Converting a JavaScript Application 261 Summary 263 Key Points 263 Chapter 10: Automated Testing 264 Framework Choices 265 Testing with Jest 265 Installing Jest 265 The First Specification 266 Driving the Implementation 267 Refactoring 270 Isolating Dependencies 273 Summary 274 Key Points 275 Appendix 1: JavaScript Quick Reference 276 Variables 276 Functions 277 Conditional Statements 278 Loops 279 Strings 280 Promises 281 Summary 282 Appendix 2: TypeScript Compiler 283 Getting Help 283 Sample File 283 Common Flags 284 Module Kind 284 ECMAScript Target Version 286 Generate Declarations 287 Remove Comments 287 Combined Output 287 Code Quality Flags 287 Defaults 288 Strict Switch 288 Catch Accidental Fallthroughs 288 Catch Missing Return Statements 289 Unused Local Variables 289 Unused Parameters 290 JavaScript Compilation 290 Allow JavaScript 290 Check JavaScript 290 Appendix 3: Bitwise Flags 291 Bit Flags Explained 292 Bitwise Operations 292 Appendix 4: Coding Katas 294 Performing a Kata 295 Example: The Fizz Buzz Kata 296 Common Coding Kata Rules 296 Summary 297 Index 298 Annotation Explore the features of this innovative open source language in depth, from working with the type system through object-orientation to understanding the runtime and the TypeScript compiler. This fully revised and updated second edition of Steve Fenton's popular book covers everything you need to discover this fascinating language and transform your experience of JavaScript development. What's New in This EditionCoverage of major changes to modules, namespaces, and module loadingNew guidance on how to use inference to reduce the effort of using TypeScriptRecommendations on compiler optionsA wide range of feature updates from intersections and tuples to async/await and the new approach to mixinsWhat You'll LearnUnderstand the TypeScript type system, and how to use it effectivelyApply object-oriented design using TypeScriptUse modules effectively to manage large programsIntegrate existing frameworks and libraries into your TypeScript programWho This Book Is ForWeb developers looking for a modern approach to JavaScript development "Explore the features of this innovative open source language in depth, from working with the type system through object-orientation to understanding the runtime and the TypeScript compiler. This fully revised and updated second edition of Steve Fenton's popular book covers everything you need to discover this fascinating language and transform your experience of JavaScript development. What's New in This Edition: Coverage of major changes to modules, namespaces, and module loading; New guidance on how to use inference to reduce the effort of using TypeScript; Recommendations on compiler options; A wide range of feature updates from intersections and tuples to async/await and the new approach to mixins. What You'll Learn: Understand the TypeScript type system, and how to use it effectively; Apply object-oriented design using TypeScript; Use modules effectively to manage large programs; Integrate existing frameworks and libraries into your TypeScript program."--Page 4 de la couverture Front Matter ....Pages i-xxvii TypeScript Language Features (Steve Fenton)....Pages 1-62 Code Organization (Steve Fenton)....Pages 63-82 The Type System (Steve Fenton)....Pages 83-97 Object Orientation in TypeScript (Steve Fenton)....Pages 99-127 Understanding the Runtime (Steve Fenton)....Pages 129-157 Running TypeScript in a Browser (Steve Fenton)....Pages 159-193 Running TypeScript on a Server (Steve Fenton)....Pages 195-216 Exceptions, Memory, and Performance (Steve Fenton)....Pages 217-232 Using JavaScript Libraries (Steve Fenton)....Pages 233-243 Automated Testing (Steve Fenton)....Pages 245-256 Back Matter ....Pages 257-287