Releases: danvk/effective-typescript
Releases · danvk/effective-typescript
Second Edition
[Released] May 2024.
Highlights
- There's substantially more book! 250 → 375 pages, 62 → 83 items
- Code samples use Twoslash syntax to indicate the types of symbols. This better aligns with the TypeScript Playground.
- There are many more references to relevant eslint / typescript-eslint rules throughout the book.
- This repo now includes a Markdown file for each item including "Things to Remember" and a full list of code samples with TypeScript playground links.
Detailed Diff
- 📖 New Chapters:
- Chapter 6: Generics and Type-Level Programming
- Chapter 7: TypeScript Recipes
- ➕ New Items
- Item 28: Use Classes and Currying to Create New Inference Sites
- Item 32: Avoid Including null or undefined in Type Aliases
- Item 36: Use a Distinct Type for Special Values
- Item 37: Limit the Use of Optional Properties
- Item 38: Avoid Repeated Parameters of the Same Type
- Item 39: Prefer Unifying Types to Modeling Differences
- Item 48: Avoid Soundness Traps
- Item 50: Think of Generics as Functions Between Types
- Item 51: Avoid Unnecessary Type Parameters
- Item 53: Know how to Control the Distribution of Unions over Conditional Types
- Item 54: Use Template Literal Types to Model DSLs and Relationships Between Strings
- Item 56: Pay Attention to how Types Display
- Item 57: Prefer Tail-Recursive Generic Types
- Item 58: Consider Codegen As an Alternative to Complex Types
- Item 59: Use Never Types to Perform Exhaustiveness Checking
- Item 62: Use Rest Parameters and Tuple Types to Model Variadic Functions
- Item 63: Use Optional Never Properties to Model Exclusive Or
- Item 71: Use Module Augmentation to Improve Types
- Item 74: Know How to Reconstruct Types at Runtime
- Item 76: Create an Accurate Model of Your Environment
- Item 77: Understand the Relationship Between Type Checking and Unit Testing
- Item 78: Pay Attention to Compiler Performance
- ➖ Dropped Items
- Item 56: Don't Rely on Private to Hide Information
- 🚧 Items with Major Changes
- Item 13: Know the Differences Between type and interface
- Changed the conclusion to favor
interface
. - Added example of type inlining, removed functions w/ properties.
- Changed the conclusion to favor
- Item 14: Use readonly to Avoid Errors Associated with Mutation (was Item 17)
- Added more discussion of
readonly
property modifier as a lead-in toReadonlyArray
- Added more discussion of
- Item 15: Use Type Operations and Generic Types to Avoid Repeating Yourself
- Was Item 14: Use Type Operations and Generics to Avoid Repeating Yourself
- Removed most material on generics.
- Reworked many examples.
- Added deeper discussion of mapped types including homomorphic mapped types and "as" clause.
- Item 16: Prefer More Precise Alternatives to Index Signatures
- Was Item 15: Use Index Signatures for Dynamic Data
- Reframed as "Prefer more Precise Alternatives to Index Signatures".
- Expanded discussion of ES6 Map.
- Added template literal type example.
- Item 17: Avoid Numeric Index Signatures
- Was Item 16: Prefer Arrays, Tuples, and ArrayLike to number Index Signatures
- Reframed as "Avoid Numeric Index Signatures"
- Updated examples
- Added discussion of iterables/generators
- Item 20: Understand How a Variable Gets Its Type
- Was Item 21: Understand Type Widening
- Used more official terminology.
- Added
tuple()
helper. - Added discussion of
satisfies
operator.
- Item 42: Avoid Types Based on Anecdotal Data
- Was Item 35: Generate Types from APIs and Specs, Not Data
- Focused more on "avoid types based on anecdotal data."
- Replaced GraphQL example w/ OpenAPI.
- Item 45: Hide Unsafe Type Assertions in Well-Typed Functions (was Item 40)
- Ditched
cacheLast
(memoize) example in favor of fetching JSON
- Ditched
- Item 47: Prefer Type-Safe Approaches to Monkey Patching (was Item 43)
- Focused on modeling global
userFromPageLoad
example
- Focused on modeling global
- Item 55: Write Tests for Your Types
- Was Item 52: Be Aware of the Pitfalls of Testing Types
- Nearly complete rewrite.
- Added discussion of
expect-type
, the Type Challenges approach, andeslint-plugin-expect-type
.
- Item 70: Mirror Types to Sever Dependencies (was Item 51)
- The conclusion stays the same but the whole argument has changed.
- Item 72: Prefer ECMAScript Features to TypeScript Features (was Item 53)
- Updated discussion of decorators
- Added TypeScript
private
visibility modifier, which was formerly its own item: "Item 56: Don't Rely on Private to Hide Information."
- Item 73: Use Source Maps to Debug TypeScript (was Item 57)
- Added discussion of debugging Node.js code.
- Item 79: Write Modern JavaScript (was Item 58)
- Focused on imports and classes
- Moved most of the old sections into a bulleted list
- Item 13: Know the Differences Between type and interface
- ✏️ Items with Minor Changes
- 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
- 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 Annotations to Type Assertions (was Item 9: Prefer Type Declarations to Type Assertions)
- Item 10: Avoid Object Wrapper Types (String, Number, Boolean, Symbol, BigInt)
- Item 11: Distinguish Excess Property Checking from Type Checking (was Item 11: Recognize the Limits of Excess Property Checking)
- Item 12: Apply Types to Entire Function Expressions When Possible
- Item 18: Avoid Cluttering Your Code with Inferable Types (was Item 19)
- Item 19: Use Different Variables for Different Types (was Item 20)
- Item 21: Create Objects All at Once (was Item 23)
- Item 22: Understand Type Narrowing
- Item 23: Be Consistent in Your Use of Aliases (was Item 24)
- Item 24: Understand How Context Is Used in Type Inference (was Item 26)
- Item 25: Understand Evolving Types (was Item 41: Understand Evolving any)
- Item 26: Use Functional Constructs and Libraries to Help Types Flow (was Item 27)
- Item 27: Use async Functions Instead of Callbacks to Improve Type Flow (was Item 25: Use async Functions Instead of Callbacks for Asynchronous Code)
- Item 29: Prefer Types That Always Represent Valid States (was Item 28)
- Item 30: Be Liberal in What You Accept and Strict in What You Produce (was Item 29)
- Item 31: Don’t Repeat Type Information in Documentation (was Item 30)
- Item 33: Push Null Values to the Perimeter of Your Types (was Item 31)
- Item 34: Prefer Unions of Interfaces to Interfaces with Unions (was Item 32: Prefer Unions of Interfaces to Interfaces of Unions)
- Item 35: Prefer More Precise Alternatives to String Types (was Item 33)
- Item 40: Prefer Imprecise Types to Inaccurate Types (was Item 34: Prefer Incomplete Types to Inaccurate Types)
- Item 41: Name Types Using the Language of Your Problem Domain (was Item 36)
- Item 43: Use the Narrowe...
First Edition
Released October 2019