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