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.
• Learn the nuts and bolts of TypeScript's type system
• Use type inference to get full safety with a minimum of type annotations
• Design types to make your code safer and more understandable
• Model complex APIs using generic types and type-level programming
• Understand how dependencies and type declaration files work in TypeScript
• Successfully migrate your JavaScript code base to TypeScript
Author(s): Dan Vanderkam
Edition: 2
Publisher: O'Reilly Media
Year: 2024
Language: English
Commentary: Publisher's PDF
Pages: 401
City: Sebastopol, CA
Tags: Web Applications; Best Practices; TypeScript; Type Inference
Cover
Copyright
Table of Contents
Preface to the Second Edition
Who This Book Is For
Why I Wrote This Book
How This Book Is Organized
Conventions in TypeScript Code Samples
Typographical Conventions Used in This Book
Using Code Examples
O’Reilly Online Learning
How to Contact Us
Acknowledgments
Preface to the First Edition (2019)
Acknowledgments to the First Edition
Chapter 1. Getting to Know TypeScript
Item 1: Understand the Relationship Between TypeScript and JavaScript
Things to Remember
Item 2: Know Which TypeScript Options You’re Using
noImplicitAny
strictNullChecks
Other Options
Things to Remember
Item 3: Understand That Code Generation Is Independent of Types
You Cannot Check TypeScript Types at Runtime
Code with Type Errors Can Produce Output
Type Operations Cannot Affect Runtime Values
Runtime Types May Not Be the Same as Declared Types
You Cannot Overload a Function Based on TypeScript Types
TypeScript Types Have No Effect on Runtime Performance
Things to Remember
Item 4: Get Comfortable with Structural Typing
Things to Remember
Item 5: Limit Use of the any Type
There’s No Type Safety with any Types
any Lets You Break Contracts
There Are No Language Services for any Types
any Types Mask Bugs When You Refactor Code
any Hides Your Type Design
any Undermines Confidence in the Type System
Things to Remember
Chapter 2. TypeScript’s Type System
Item 6: Use Your Editor to Interrogate and Explore the Type System
Things to Remember
Item 7: Think of Types as Sets of Values
Things to Remember
Item 8: Know How to Tell Whether a Symbol Is in the Type Space or Value Space
Things to Remember
Item 9: Prefer Type Annotations to Type Assertions
Things to Remember
Item 10: Avoid Object Wrapper Types (String, Number, Boolean, Symbol, BigInt)
Things to Remember
Item 11: Distinguish Excess Property Checking from Type Checking
Things to Remember
Item 12: Apply Types to Entire Function Expressions When Possible
Things to Remember
Item 13: Know the Differences Between type and interface
Things to Remember
Item 14: Use readonly to Avoid Errors Associated with Mutation
Things to Remember
Item 15: Use Type Operations and Generic Types to Avoid Repeating Yourself
Things to Remember
Item 16: Prefer More Precise Alternatives to Index Signatures
Things to Remember
Item 17: Avoid Numeric Index Signatures
Things to Remember
Chapter 3. Type Inference and Control Flow Analysis
Item 18: Avoid Cluttering Your Code with Inferable Types
Things to Remember
Item 19: Use Different Variables for Different Types
Things to Remember
Item 20: Understand How a Variable Gets Its Type
Things to Remember
Item 21: Create Objects All at Once
Things to Remember
Item 22: Understand Type Narrowing
Things to Remember
Item 23: Be Consistent in Your Use of Aliases
Things to Remember
Item 24: Understand How Context Is Used in Type Inference
Tuple Types
Objects
Callbacks
Things to Remember
Item 25: Understand Evolving Types
Things to Remember
Item 26: Use Functional Constructs and Libraries to Help Types Flow
Things to Remember
Item 27: Use async Functions Instead of Callbacks to Improve Type Flow
Things to Remember
Item 28: Use Classes and Currying to Create New Inference Sites
Classes
Currying
Things to Remember
Chapter 4. Type Design
Item 29: Prefer Types That Always Represent Valid States
Things to Remember
Item 30: Be Liberal in What You Accept and Strict in What You Produce
Things to Remember
Item 31: Don’t Repeat Type Information in Documentation
Things to Remember
Item 32: Avoid Including null or undefined in Type Aliases
Things to Remember
Item 33: Push Null Values to the Perimeter of Your Types
Things to Remember
Item 34: Prefer Unions of Interfaces to Interfaces with Unions
Things to Remember
Item 35: Prefer More Precise Alternatives to String Types
Things to Remember
Item 36: Use a Distinct Type for Special Values
Things to Remember
Item 37: Limit the Use of Optional Properties
Things to Remember
Item 38: Avoid Repeated Parameters of the Same Type
Things to Remember
Item 39: Prefer Unifying Types to Modeling Differences
Things to Remember
Item 40: Prefer Imprecise Types to Inaccurate Types
Things to Remember
Item 41: Name Types Using the Language of Your Problem Domain
Things to Remember
Item 42: Avoid Types Based on Anecdotal Data
Things to Remember
Chapter 5. Unsoundness and the any Type
Item 43: Use the Narrowest Possible Scope for any Types
Things to Remember
Item 44: Prefer More Precise Variants of any to Plain any
Things to Remember
Item 45: Hide Unsafe Type Assertions in Well-Typed Functions
Things to Remember
Item 46: Use unknown Instead of any for Values with an Unknown Type
Things to Remember
Item 47: Prefer Type-Safe Approaches to Monkey Patching
Things to Remember
Item 48: Avoid Soundness Traps
any
Type Assertions
Object and Array Lookups
Inaccurate Type Definitions
Bivariance in Class Hierarchies
TypeScript’s Inaccurate Model of Variance for Objects and Arrays
Function Calls Don’t Invalidate Refinements
Assignability and Optional Properties
Things to Remember
Item 49: Track Your Type Coverage to Prevent Regressions in Type Safety
Things to Remember
Chapter 6. Generics and Type-Level Programming
Item 50: Think of Generics as Functions Between Types
Things to Remember
Item 51: Avoid Unnecessary Type Parameters
Things to Remember
Item 52: Prefer Conditional Types to Overload Signatures
Things to Remember
Item 53: Know How to Control the Distribution of Unions over Conditional Types
Things to Remember
Item 54: Use Template Literal Types to Model DSLs and Relationships Between Strings
Things to Remember
Item 55: Write Tests for Your Types
Things to Remember
Item 56: Pay Attention to How Types Display
Things to Remember
Item 57: Prefer Tail-Recursive Generic Types
Things to Remember
Item 58: Consider Codegen as an Alternative to Complex Types
Things to Remember
Chapter 7. TypeScript Recipes
Item 59: Use Never Types to Perform Exhaustiveness Checking
Things to Remember
Item 60: Know How to Iterate Over Objects
Things to Remember
Item 61: Use Record Types to Keep Values in Sync
Things to Remember
Item 62: Use Rest Parameters and Tuple Types to Model Variadic Functions
Things to Remember
Item 63: Use Optional Never Properties to Model Exclusive Or
Things to Remember
Item 64: Consider Brands for Nominal Typing
Things to Remember
Chapter 8. Type Declarations and @types
Item 65: Put TypeScript and @types in devDependencies
Things to Remember
Item 66: Understand the Three Versions Involved in Type Declarations
Things to Remember
Item 67: Export All Types That Appear in Public APIs
Things to Remember
Item 68: Use TSDoc for API Comments
Things to Remember
Item 69: Provide a Type for this in Callbacks if It’s Part of Their API
Things to Remember
Item 70: Mirror Types to Sever Dependencies
Things to Remember
Item 71: Use Module Augmentation to Improve Types
Things to Remember
Chapter 9. Writing and Running Your Code
Item 72: Prefer ECMAScript Features to TypeScript Features
Enums
Parameter Properties
Namespaces and Triple-Slash Imports
experimentalDecorators
Member Visibility Modifiers (Private, Protected, and Public)
Things to Remember
Item 73: Use Source Maps to Debug TypeScript
Things to Remember
Item 74: Know How to Reconstruct Types at Runtime
Generate the Types from Another Source
Define Types with a Runtime Library
Generate Runtime Values from Your Types
Things to Remember
Item 75: Understand the DOM Hierarchy
Things to Remember
Item 76: Create an Accurate Model of Your Environment
Things to Remember
Item 77: Understand the Relationship Between Type Checking and Unit Testing
Things to Remember
Item 78: Pay Attention to Compiler Performance
Separate Type Checking from Building
Prune Unused Dependencies and Dead Code
Incremental Builds and Project References
Simplify Your Types
Things to Remember
Chapter 10. Modernization and Migration
Item 79: Write Modern JavaScript
Use ECMAScript Modules
Use Classes Instead of Prototypes
Other Features
Things to Remember
Item 80: Use @ts-check and JSDoc to Experiment with TypeScript
Undeclared Globals
Unknown Libraries
DOM Issues
Inaccurate JSDoc
Things to Remember
Item 81: Use allowJs to Mix TypeScript and JavaScript
Things to Remember
Item 82: Convert Module by Module Up Your Dependency Graph
Undeclared Class Members
Values with Changing Types
Things to Remember
Item 83: Don’t Consider Migration Complete Until You Enable noImplicitAny
Things to Remember
Appendix. Item Mapping Between
Index
About the Author
Colophon