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
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
【One-Line Pitch】
A practical, item-based guide for TypeScript developers who want to move beyond the basics and master the language's type system, inference, and design patterns—ideal for anyone writing real-world TypeScript who wants fewer bugs and more maintainable code.
【Book Arc】
- **Opening (~0%–10%)**: Introduces TypeScript as a typed superset of JavaScript, explains the philosophy of modeling JavaScript's runtime behavior, and covers the fundamentals of the type system—including structural typing, the dangers of `any`, and how types relate to sets of values.
- **Early (~10%–23%)**: Dives into the nuances of type design—excess property checking, the differences between `interface` and `type`, `readonly` modifiers, and index signatures—showing how to model data safely and avoid common pitfalls.
- **Early (~23%–32%)**: Shifts to type inference and control flow analysis, teaching when to rely on inference versus explicit annotations, how to use different variables for different types, and how to create objects safely.
- **Middle (~32%–48%)**: Explores advanced inference techniques, including contextual typing, functional constructs like Lodash chains, and generic types for modeling complex APIs—plus practical advice on naming, null handling, and avoiding type aliases that include `null` or `undefined`.
- **Late (~48%–end)**: Covers type-level programming, TypeScript recipes, migration strategies from JavaScript, and performance considerations—helping you apply everything in real projects and scale your TypeScript knowledge.
【Key Takeaways】
- **Structural typing is the core mental model** (Early): TypeScript checks compatibility based on shape, not declared relationships—so a `NamedVector` works where a `Vector2D` is expected. This mirrors JavaScript's runtime behavior but can lead to subtle bugs, like `normalize` accepting a 3D vector and returning wrong results.
- **Avoid `any` at all costs** (Early): `any` eliminates type safety, breaks contracts, harms developer experience, makes refactoring error-prone, and undermines confidence in the type system. Use it only as a last resort.
- **Excess property checking is a separate process from assignability** (Early): It catches typos like `darkmode` instead of `darkMode` in object literals, but it has limits—understanding this distinction builds a clearer mental model of structural typing.
- **Prefer `interface` over `type` for most cases** (Early): They're nearly interchangeable, but `interface` supports declaration merging and is more idiomatic. Avoid prefixing with `I` or `T`—it's unnecessary and considered bad style today.
- **`readonly` is shallow** (Early): It prevents reassignment but not mutation of nested properties. For deep readonly types, use a library rather than rolling your own—it's tricky to get right.
- **Use type inference to minimize annotations** (Early): Experienced developers use few annotations but to great effect. Don't annotate return types unless the function has multiple returns, is part of a public API, or you want a named type.
- **Use different variables for different types** (Early): Reusing a variable for differently typed values causes errors in TypeScript. Create objects all at once to avoid type drift and keep your code safe.
- **Context is key in type inference** (Middle): When passing callbacks inline, TypeScript infers parameter types from context. Factoring callbacks out into constants loses this context and triggers `noImplicitAny` errors—prefer inline forms when possible.
【Reading Tips】
- **Skim the opening chapters** (~0%–10%) if you're already comfortable with TypeScript basics—they cover fundamentals you likely know, but the set-theory framing of types (Item 7) is worth a deep read.
- **Deep-read the type design chapters** (~10%–23%): These items on `interface` vs. `type`, `readonly`, and index signatures are where you'll build the mental models that prevent real bugs.
- **Pay special attention to control flow analysis** (~23%–32%): This is where beginners often struggle—understanding when to rely on inference versus annotations will dramatically improve your code quality.
- **Use the "Things to Remember" summaries** at the end of each item as quick reference—they distill the key advice into actionable rules.
- **Read the migration and performance chapters** (~48%–end) if you're planning to convert a JavaScript codebase or dealing with slow compilation—these are practical, real-world concerns.
【Coverage Limits】
This guide synthesizes the first ~48% of the book in detail; the later chapters on type-level programming, recipes, and migration are covered only at a high level based on the blurb and early excerpts.
Page 20
s inspired by a discussion with Evan Martin on reddit and a frustrating bug that came back to new Set("string"). This led to the blog post “In Defense of Int...
union) T1 & T2 T1 ∩ T2 (intersection) unknown Universal set There’s an important caveat to this interpretation: it works best when you think of values as imm...
eginner may drown their code in redundant type annotations. In most languages, a variable has a type and it never changes. TypeScript is a bit different. A v...
etch('/seeds'); // ~~~~ Seed[] is not assignable to Seed This produces exactly the errors we were hoping for. (You also need to implement the class, of co...
ve left pizza with a Pizza type in eatDinner, it would have prevented type checking on this parameter for all calls to eatSalad in your program, not just thi...
runtime errors despite your code passing the type checker. The solution is usually to update your type declarations so that the versions are back in sync. If...
rg/en/docs/inspector Now you can open your browser to debug. In Chrome, for example, you navigate to chrome://inspect. You should see a remote target that yo...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Add Tag
Enter tag name (max 50 characters)
Share E-Book
Effective TypeScript (Dan Vanderkam) (Z-Library)
Scan QR code with your phone to access
Copy the link or scan the QR code to access this e-book on your phone
Share E-Book via Email
Please enter email address
Donation Statistics
¥.00
Total Donations
0
Donation Count
Effective TypeScript (Dan Vanderkam) (Z-Library)
Find Your Favorite Books
Only registered users can comment after logging in. Comments need to be reviewed by administrators before being displayed
Loading comments...
Reply to Comment
Edit Comment