Preface to the Second Edition....8
Who This Book Is For....9
Why I Wrote This Book....10
How This Book Is Organized....11
Conventions in TypeScript Code Samples....13
Typographical Conventions Used in This Book....15
Using Code Examples....16
OReilly Online Learning....16
How to Contact Us....17
Acknowledgments....17
Preface to the First Edition (2019)....21
Acknowledgments to the First Edition....22
1. Getting to Know TypeScript....25
Item 1: Understand the Relationship Between TypeScript and JavaScript....25
Item 2: Know Which TypeScript Options Youre Using....33
Item 3: Understand That Code Generation Is Independent of Types....39
Item 4: Get Comfortable with Structural Typing....48
Item 5: Limit Use of the any Type....53
2. TypeScripts Type System....60
Item 6: Use Your Editor to Interrogate and Explore the Type System....60
Item 7: Think of Types as Sets of Values....67
Item 8: Know How to Tell Whether a Symbol Is in the Type Space or Value Space....79
Item 9: Prefer Type Annotations to Type Assertions....86
Item 10: Avoid Object Wrapper Types (String, Number, Boolean, Symbol, BigInt)....92
Item 11: Distinguish Excess Property Checking from Type Checking....96
Item 12: Apply Types to Entire Function Expressions When Possible....101
Item 13: Know the Differences Between type and interface....106
Item 14: Use readonly to Avoid Errors Associated with Mutation....114
Item 15: Use Type Operations and Generic Types to Avoid Repeating Yourself....122
Item 16: Prefer More Precise Alternatives to Index Signatures....134
Item 17: Avoid Numeric Index Signatures....140
3. Type Inference and Control Flow Analysis....145
Item 18: Avoid Cluttering Your Code with Inferable Types....146
Item 19: Use Different Variables for Different Types....155
Item 20: Understand How a Variable Gets Its Type....158
Item 21: Create Objects All at Once....165
Item 22: Understand Type Narrowing....168
Item 23: Be Consistent in Your Use of Aliases....174
Item 24: Understand How Context Is Used in Type Inference....179
Item 25: Understand Evolving Types....185
Item 26: Use Functional Constructs and Libraries to Help Types Flow....189
Item 27: Use async Functions Instead of Callbacks to Improve Type Flow....195
Item 28: Use Classes and Currying to Create New Inference Sites....201
4. Type Design....207
Item 29: Prefer Types That Always Represent Valid States....207
Item 30: Be Liberal in What You Accept and Strict in What You Produce....214
Item 31: Dont Repeat Type Information in Documentation....219
Item 32: Avoid Including null or undefined in Type Aliases....222
Item 33: Push Null Values to the Perimeter of Your Types....224
Item 34: Prefer Unions of Interfaces to Interfaces with Unions....229
Item 35: Prefer More Precise Alternatives to String Types....234
Item 36: Use a Distinct Type for Special Values....240
Item 37: Limit the Use of Optional Properties....244
Item 38: Avoid Repeated Parameters of the Same Type....248
Item 39: Prefer Unifying Types to Modeling Differences....251
Item 40: Prefer Imprecise Types to Inaccurate Types....254
Item 41: Name Types Using the Language of Your Problem Domain....260
Item 42: Avoid Types Based on Anecdotal Data....263
5. Unsoundness and the any Type....270
Item 43: Use the Narrowest Possible Scope for any Types....270
Item 44: Prefer More Precise Variants of any to Plain any....275
Item 45: Hide Unsafe Type Assertions in Well-Typed Functions....277
Item 46: Use unknown Instead of any for Values with an Unknown Type....283
Item 47: Prefer Type-Safe Approaches to Monkey Patching....288
Item 48: Avoid Soundness Traps....293
Item 49: Track Your Type Coverage to Prevent Regressions in Type Safety....305
6. Generics and Type-Level Programming....310
Item 50: Think of Generics as Functions Between Types....311
Item 51: Avoid Unnecessary Type Parameters....318
Item 52: Prefer Conditional Types to Overload Signatures....326
Item 53: Know How to Control the Distribution of Unions over Conditional Types....330
Item 54: Use Template Literal Types to Model DSLs and Relationships Between Strings....336
Item 55: Write Tests for Your Types....344
Item 56: Pay Attention to How Types Display....355
Item 57: Prefer Tail-Recursive Generic Types....360
Item 58: Consider Codegen as an Alternative to Complex Types....365
7. TypeScript Recipes....371
Item 59: Use Never Types to Perform Exhaustiveness Checking....371
Item 60: Know How to Iterate Over Objects....379
Item 61: Use Record Types to Keep Values in Sync....383
Item 62: Use Rest Parameters and Tuple Types to Model Variadic Functions....387
Item 63: Use Optional Never Properties to Model Exclusive Or....391
Item 64: Consider Brands for Nominal Typing....394
8. Type Declarations and types....400
Item 65: Put TypeScript and types in devDependencies....400
Item 66: Understand the Three Versions Involved in Type Declarations....403
Item 67: Export All Types That Appear in Public APIs....409
Item 68: Use TSDoc for API Comments....410
Item 69: Provide a Type for this in Callbacks if Its Part of Their API....414
Item 70: Mirror Types to Sever Dependencies....420
Item 71: Use Module Augmentation to Improve Types....423
9. Writing and Running Your Code....429
Item 72: Prefer ECMAScript Features to TypeScript Features....429
Item 73: Use Source Maps to Debug TypeScript....439
Item 74: Know How to Reconstruct Types at Runtime....446
Item 75: Understand the DOM Hierarchy....453
Item 76: Create an Accurate Model of Your Environment....460
Item 77: Understand the Relationship Between Type Checking and Unit Testing....463
Item 78: Pay Attention to Compiler Performance....468
10. Modernization and Migration....478
Item 79: Write Modern JavaScript....479
Item 80: Use ts-check and JSDoc to Experiment with TypeScript....485
Item 81: Use allowJs to Mix TypeScript and JavaScript....491
Item 82: Convert Module by Module Up Your Dependency Graph....493
Item 83: Dont Consider Migration Complete Until You Enable noImplicitAny....501
Appendix. Item Mapping Between First and Second Editions....505
Index....512
About the Author....567
TypeScript is a typed superset of JavaScript with the potential to solve many of the headaches for which JavaScript is famous. But TypeScript has a learning curve of its own, and understanding how to use it effectively takes time and practice. Using the format popularized by Effective C++ and Effective Java (both Addison-Wesley), this practical book features 83 items that give specific advice on what to do and what not to do, and how to think about the language.
Author Dan Vanderkam shows you how to apply each item's advice through concrete examples. This book will help you advance from a beginning or intermediate user familiar with TypeScript basics to an expert who knows how to use the language well.
Updated for TypeScript 5, this second edition includes two new chapters on type-level programming and TypeScript recipes.