Share E-Book
Scan to open this page

Scan with your phone to open this page

AuthorDan Vanderkam

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 70+ 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. Learn the nuts and bolts of TypeScript's type system Design types to make your code safer and more understandable Avoid common gotchas in TypeScript Learn how to write idiomatic TypeScript Make tactical use of the any type Understand how dependencies and type declaration files work in TypeScript Learn how to program at the type level using generic types Successfully migrate your JavaScript codebase to TypeScript Updated for TypeScript 5, this updated second edition includes a new chapter on type-level programming.

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
# Effective TypeScript: 83 Specific Ways to Improve Your TypeScript, 2nd Edition ## 【One-Line Pitch】 A practical, item-based guide that takes you from TypeScript basics to expert-level mastery, covering type system nuances, design patterns, and migration strategies. Essential reading for JavaScript developers and TypeScript users who want to write safer, more idiomatic code. ## 【Book Arc】 - **Opening (~0%–10%)**: Introduces the book's structure—83 "items" grouped into thematic chapters, each with a title conveying the key takeaway, concrete examples, and "Things to Remember" summaries. Establishes TypeScript as a typed superset of JavaScript with a gentle migration path. - **Early (~10%–23%)**: Covers TypeScript's relationship with JavaScript, the critical `noImplicitAny` and `strictNullChecks` compiler options, and the dangers of the `any` type. Explains how `any` eliminates type safety, breaks contracts, and disables language services like autocomplete. - **Early (~23%–32%)**: Delves into TypeScript's type system fundamentals—type space vs. value space, `typeof` in both contexts, property accessor syntax, and the pitfalls of object wrapper types (String, Number, Boolean). Includes practical debugging with the language service's refactoring tools. - **Middle (~32%–42%)**: Explores type operations and generic types to reduce repetition, declaration merging for modeling JavaScript's evolving standard library, and the differences between interfaces and type aliases. Introduces `readonly` parameters for clearer contracts. - **Middle (~42%–48%)**: Covers mapped types, template literal types, and homomorphic type transformations. Discusses modeling dynamic data with `Map` instead of index signatures, and runtime validation patterns for converting broad types to specific ones. - **Late (~48%–100%)**: The excerpts cover up to ~48% of the book; later chapters (not fully covered here) include type-level programming, dependency management, type declaration files, and JavaScript-to-TypeScript migration strategies. ## 【Key Takeaways】 - **Compiler strictness is your friend** (Early): Enable `noImplicitAny` and `strictNullChecks` from the start—they catch errors early, improve readability, and make TypeScript feel like a different, safer language. (Early) - **`any` is a dangerous escape hatch** (Early): Using `any` eliminates type safety, breaks function contracts, and disables autocomplete and documentation. Use it sparingly and understand its consequences before resorting to it. (Early) - **Type space vs. value space is a core mental model** (Early): `typeof` means different things in type vs. value contexts, and `obj['field']` vs. `obj.field` are not equivalent in type space. Mastering this distinction is key to advanced TypeScript. (Early) - **Avoid object wrapper types** (Early): `String`, `Number`, `Boolean`, `Symbol`, and `BigInt` wrappers behave unexpectedly (e.g., `"hello" === new String("hello")` is `false`). Stick with primitives. (Early) - **`readonly` parameters improve contracts** (Middle): Marking parameters `readonly` catches accidental mutations, advertises intent to callers, and accepts a broader set of input types. It's contagious in a good way—spread it through your codebase. (Middle) - **Declaration merging models library evolution** (Middle): TypeScript uses interface merging to add ES2015+ methods to the `Array` type based on your `target` setting. This keeps types accurate for the JavaScript version you're targeting. (Middle) - **Mapped types and generics eliminate repetition** (Middle): Use type operations like `Pick`, template literal types, and homomorphic mapped types to transform types systematically and keep your code DRY. (Middle) - **Validate dynamic data at runtime** (Middle): When parsing external data, validate against a broad type (like `Map<string, string>`) first, then narrow to a specific type (like `Rocket`). This catches errors at load time, not later. (Middle) ## 【Reading Tips】 - **Skim the item titles first**: Each title is the key takeaway. Read the table of contents to internalize the advice before diving into details. - **Focus on the examples**: The book is example-driven. If you're short on time, read the code samples and the "Things to Remember" bullets at the end of each item. - **Deep-read the type system chapters (Items 5–15)**: These are the foundation for everything else. Understanding `any` dangers, type space vs. value space, and mapped types will make later chapters much easier. - **Jump around as needed**: The items are self-contained. If you're working on a specific problem (e.g., migration, type-level programming), find the relevant chapter and read only those items. - **Apply the advice incrementally**: Start with `noImplicitAny` and `strictNullChecks` on new projects, then gradually adopt `readonly` parameters and avoid `any` as you gain confidence. ## 【Coverage Limits】 The excerpts cover the book's opening through approximately 48% (Chapters 1–3). Later chapters on type-level programming, dependency management, type declarations, and JavaScript migration strategies are not covered in this guide. ##
Page 15
t gives you specific advice about some aspect of TypeScript. The items are grouped themati‐ cally into chapters, but feel free to jump around and read whiche...
View in text
Excerpt 2
would have occurred had you used undefined instead of null. Item 2: Know Which TypeScript Options You’re Using | 9 “as number” is a type assertion, sometimes...
View in text
Excerpt 3
efined, symbol, and bigint. The first five have been around since the beginning. The symbol primitive was added in ES2015, and bigint joined the family with...
View in text
Excerpt 4
and 33), these sorts of implicit understandings can lead to trouble with type checking. Better to make them explicit, both for human readers and for tsc. Thi...
View in text
Excerpt 5
s to an object and allowing TypeScript to infer a new type. To conditionally add a property in a type-safe way, you can use spread syntax with {} or any fals...
View in text
Excerpt 6
y'); // ~~~~~~~ Expected 2 type arguments, but got 1. The problem is that type inference in TypeScript is an all or nothing affair: either you can let TypeSc...
View in text
Excerpt 7
ement, an elevation, and potentially more. In an attempt to make the type declarations more precise, you’ve gone too far and made the types inaccurate! To co...
View in text
Excerpt 8
in b` return false; } } return Object.keys(a).length === Object.keys(b).length; } This any is narrowly scoped (Item 43), does not affect the type signature o...
View in text
Tags
AI categories
Programming LanguageTypeScriptCode
ISBN: 1098155068
Publisher: O'Reilly Media
Publish Year: 2024
Language: English
Pages: 404
File Format: PDF
File Size: 6.2 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…