Effective TypeScript

Book description

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 can take time. This book guides you through 62 specific ways to improve your use of TypeScript.

Author Dan Vanderkam, a principal software engineer at Sidewalk Labs, shows you how to apply these ideas, following the format popularized by Effective C++ and Effective Java (both from Addison-Wesley). You’ll advance from a beginning or intermediate user familiar with the basics to an advanced user who knows how to use the language well.

Effective TypeScript is divided into eight chapters:

  • Getting to Know TypeScript
  • TypeScript’s Type System
  • Type Inference
  • Type Design
  • Working with any
  • Types Declarations and @types
  • Writing and Running Your Code
  • Migrating to TypeScript

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Who This Book Is For
    2. Why I Wrote This Book
    3. How This Book Is Organized
    4. Conventions in TypeScript Code Samples
    5. Typographical Conventions Used in This Book
    6. Using Code Examples
    7. O’Reilly Online Learning
    8. How to Contact Us
    9. Acknowledgments
  2. 1. Getting to Know TypeScript
    1. Item 1: Understand the Relationship Between TypeScript and JavaScript
    2. Item 2: Know Which TypeScript Options You’re Using
    3. Item 3: Understand That Code Generation Is Independent of Types
    4. Item 4: Get Comfortable with Structural Typing
    5. Item 5: Limit Use of the any Type
  3. 2. TypeScript’s Type System
    1. Item 6: Use Your Editor to Interrogate and Explore the Type System
    2. Item 7: Think of Types as Sets of Values
    3. Item 8: Know How to Tell Whether a Symbol Is in the Type Space or Value Space
    4. Item 9: Prefer Type Declarations to Type Assertions
    5. Item 10: Avoid Object Wrapper Types (String, Number, Boolean, Symbol, BigInt)
    6. Item 11: Recognize the Limits of Excess Property Checking
    7. Item 12: Apply Types to Entire Function Expressions When Possible
    8. Item 13: Know the Differences Between type and interface
    9. Item 14: Use Type Operations and Generics to Avoid Repeating Yourself
    10. Item 15: Use Index Signatures for Dynamic Data
    11. Item 16: Prefer Arrays, Tuples, and ArrayLike to number Index Signatures
    12. Item 17: Use readonly to Avoid Errors Associated with Mutation
    13. Item 18: Use Mapped Types to Keep Values in Sync
  4. 3. Type Inference
    1. Item 19: Avoid Cluttering Your Code with Inferable Types
    2. Item 20: Use Different Variables for Different Types
    3. Item 21: Understand Type Widening
    4. Item 22: Understand Type Narrowing
    5. Item 23: Create Objects All at Once
    6. Item 24: Be Consistent in Your Use of Aliases
    7. Item 25: Use async Functions Instead of Callbacks for Asynchronous Code
    8. Item 26: Understand How Context Is Used in Type Inference
    9. Item 27: Use Functional Constructs and Libraries to Help Types Flow
  5. 4. Type Design
    1. Item 28: Prefer Types That Always Represent Valid States
    2. Item 29: Be Liberal in What You Accept and Strict in What You Produce
    3. Item 30: Don’t Repeat Type Information in Documentation
    4. Item 31: Push Null Values to the Perimeter of Your Types
    5. Item 32: Prefer Unions of Interfaces to Interfaces of Unions
    6. Item 33: Prefer More Precise Alternatives to String Types
    7. Item 34: Prefer Incomplete Types to Inaccurate Types
    8. Item 35: Generate Types from APIs and Specs, Not Data
    9. Item 36: Name Types Using the Language of Your Problem Domain
    10. Item 37: Consider “Brands” for Nominal Typing
  6. 5. Working with any
    1. Item 38: Use the Narrowest Possible Scope for any Types
    2. Item 39: Prefer More Precise Variants of any to Plain any
    3. Item 40: Hide Unsafe Type Assertions in Well-Typed Functions
    4. Item 41: Understand Evolving any
    5. Item 42: Use unknown Instead of any for Values with an Unknown Type
    6. Item 43: Prefer Type-Safe Approaches to Monkey Patching
    7. Item 44: Track Your Type Coverage to Prevent Regressions in Type Safety
  7. 6. Types Declarations and @types
    1. Item 45: Put TypeScript and @types in devDependencies
    2. Item 46: Understand the Three Versions Involved in Type Declarations
    3. Item 47: Export All Types That Appear in Public APIs
    4. Item 48: Use TSDoc for API Comments
    5. Item 49: Provide a Type for this in Callbacks
    6. Item 50: Prefer Conditional Types to Overloaded Declarations
    7. Item 51: Mirror Types to Sever Dependencies
    8. Item 52: Be Aware of the Pitfalls of Testing Types
  8. 7. Writing and Running Your Code
    1. Item 53: Prefer ECMAScript Features to TypeScript Features
    2. Item 54: Know How to Iterate Over Objects
    3. Item 55: Understand the DOM hierarchy
    4. Item 56: Don’t Rely on Private to Hide Information
    5. Item 57: Use Source Maps to Debug TypeScript
  9. 8. Migrating to TypeScript
    1. Item 58: Write Modern JavaScript
    2. Item 59: Use @ts-check and JSDoc to Experiment with TypeScript
    3. Item 60: Use allowJs to Mix TypeScript and JavaScript
    4. Item 61: Convert Module by Module Up Your Dependency Graph
    5. Item 62: Don’t Consider Migration Complete Until You Enable noImplicitAny
  10. Index

Product information

  • Title: Effective TypeScript
  • Author(s): Dan Vanderkam
  • Release date: October 2019
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492053743