Awesome
<img src="/cover.jpg" width="286" title="Second Edition Cover Image" align="right">
Effective TypeScript
This is the code sample repo for the Effective TypeScript: 83 Specific Ways to Improve Your TypeScript (O'Reilly, May 2024). The book is available through:
and others. You can get the latest updates on the book at effectivetypescript.com.
Still on the first edition? Check out the 1st-edition
branch.
Code Samples
Below you'll find a complete table of contents. Each item links to a page with its "Things to Remember" (a short summary of the item) and a listing of all its code samples. The code samples are accompanied by links to the TypeScript playground. They should produce the expected errors (and no others) and expected types.
Unless otherwise noted in a comment at the top of the code sample, the samples are run with TypeScript's strict
setting. These were produced and checked using literate-ts and TypeScript 5.4.5.
-
Chapter 1: Getting to Know TypeScript
- :memo: Item 1: Understand the Relationship Between TypeScript and JavaScript
- :memo: Item 2: Know Which TypeScript Options You're Using
- :memo: Item 3: Understand That Code Generation Is Independent of Types
- :memo: Item 4: Get Comfortable with Structural Typing
- :memo: Item 5: Limit Use of the
any
Type
-
Chapter 2: TypeScript's Type System
- :memo: Item 6: Use Your Editor to Interrogate and Explore the Type System
- :memo: Item 7: Think of Types as Sets of Values
- :memo: Item 8: Know How to Tell Whether a Symbol Is in the Type Space or Value Space
- :memo: Item 9: Prefer Type Annotations to Type Assertions
- :memo: Item 10: Avoid Object Wrapper Types (String, Number, Boolean, Symbol, BigInt)
- :memo: Item 11: Distinguish Excess Property Checking from Type Checking
- :memo: Item 12: Apply Types to Entire Function Expressions When Possible
- :memo: Item 13: Know the Differences Between
type
andinterface
- :memo: Item 14: Use
readonly
to Avoid Errors Associated with Mutation - :memo: Item 15: Use Type Operations and Generic Types to Avoid Repeating Yourself
- :memo: Item 16: Prefer More Precise Alternatives to Index Signatures
- :memo: Item 17: Avoid Numeric Index Signatures
-
Chapter 3: Type Inference and Control Flow Analysis
- :memo: Item 18: Avoid Cluttering Your Code with Inferable Types
- :memo: Item 19: Use Different Variables for Different Types
- :memo: Item 20: Understand How a Variable Gets Its Type
- :memo: Item 21: Create Objects All at Once
- :memo: Item 22: Understand Type Narrowing
- :memo: Item 23: Be Consistent in Your Use of Aliases
- :memo: Item 24: Understand How Context Is Used in Type Inference
- :memo: Item 25: Understand Evolving Types
- :memo: Item 26: Use Functional Constructs and Libraries to Help Types Flow
- :memo: Item 27: Use async Functions Instead of Callbacks to Improve Type Flow
- :memo: Item 28: Use Classes and Currying to Create New Inference Sites
-
Chapter 4: Type Design
- :memo: Item 29: Prefer Types That Always Represent Valid States
- :memo: Item 30: Be Liberal in What You Accept and Strict in What You Produce
- :memo: Item 31: Don’t Repeat Type Information in Documentation
- :memo: Item 32: Avoid Including
null
orundefined
in Type Aliases - :memo: Item 33: Push Null Values to the Perimeter of Your Types
- :memo: Item 34: Prefer Unions of Interfaces to Interfaces with Unions
- :memo: Item 35: Prefer More Precise Alternatives to String Types
- :memo: Item 36: Use a Distinct Type for Special Values
- :memo: Item 37: Limit the Use of Optional Properties
- :memo: Item 38: Avoid Repeated Parameters of the Same Type
- :memo: Item 39: Prefer Unifying Types to Modeling Differences
- :memo: Item 40: Prefer Imprecise Types to Inaccurate Types
- :memo: Item 41: Name Types Using the Language of Your Problem Domain
- :memo: Item 42: Avoid Types Based on Anecdotal Data
-
Chapter 5: Unsoundness and the any Type
- :memo: Item 43: Use the Narrowest Possible Scope for
any
Types - :memo: Item 44: Prefer More Precise Variants of
any
to Plainany
- :memo: Item 45: Hide Unsafe Type Assertions in Well-Typed Functions
- :memo: Item 46: Use unknown Instead of
any
for Values with an Unknown Type - :memo: Item 47: Prefer Type-Safe Approaches to Monkey Patching
- :memo: Item 48: Avoid Soundness Traps
- :memo: Item 49: Track Your Type Coverage to Prevent Regressions in Type Safety
- :memo: Item 43: Use the Narrowest Possible Scope for
-
Chapter 6: Generics and Type-Level Programming
- :memo: Item 50: Think of Generics as Functions Between Types
- :memo: Item 51: Avoid Unnecessary Type Parameters
- :memo: Item 52: Prefer Conditional Types to Overload Signatures
- :memo: Item 53: Know How to Control the Distribution of Unions over Conditional Types
- :memo: Item 54: Use Template Literal Types to Model DSLs and Relationships Between Strings
- :memo: Item 55: Write Tests for Your Types
- :memo: Item 56: Pay Attention to How Types Display
- :memo: Item 57: Prefer Tail-Recursive Generic Types
- :memo: Item 58: Consider Codegen as an Alternative to Complex Types
-
Chapter 7: TypeScript Recipes
- :memo: Item 59: Use
never
Types to Perform Exhaustiveness Checking - :memo: Item 60: Know How to Iterate Over Objects
- :memo: Item 61: Use
Record
Types to Keep Values in Sync - :memo: Item 62: Use Rest Parameters and Tuple Types to Model Variadic Functions
- :memo: Item 63: Use Optional
never
Properties to Model Exclusive Or - :memo: Item 64: Consider Brands for Nominal Typing
- :memo: Item 59: Use
-
Chapter 8: Type Declarations and @types
- :memo: Item 65: Put TypeScript and
@types
indevDependencies
- :memo: Item 66: Understand the Three Versions Involved in Type Declarations
- :memo: Item 67: Export All Types That Appear in Public APIs
- :memo: Item 68: Use TSDoc for API Comments
- :memo: Item 69: Provide a Type for
this
in Callbacks if It's Part of Their API - :memo: Item 70: Mirror Types to Sever Dependencies
- :memo: Item 71: Use Module Augmentation to Improve Types
- :memo: Item 65: Put TypeScript and
-
Chapter 9: Writing and Running Your Code
- :memo: Item 72: Prefer ECMAScript Features to TypeScript Features
- :memo: Item 73: Use Source Maps to Debug TypeScript
- :memo: Item 74: Know How to Reconstruct Types at Runtime
- :memo: Item 75: Understand the DOM Hierarchy
- :memo: Item 76: Create an Accurate Model of Your Environment
- :memo: Item 77: Understand the Relationship Between Type Checking and Unit Testing
- :memo: Item 78: Pay Attention to Compiler Performance
-
Chapter 10: Modernization and Migration
- :memo: Item 79: Write Modern JavaScript
- :memo: Item 80: Use
@ts-check
and JSDoc to Experiment with TypeScript - :memo: Item 81: Use
allowJs
to Mix TypeScript and JavaScript - :memo: Item 82: Convert Module by Module Up Your Dependency Graph
- :memo: Item 83: Don't Consider Migration Complete Until You Enable
noImplicitAny