Book description
TypeScript is one of the most important tools for JavaScript developers. Still, even experienced developers wonder why the TypeScript compiler is throwing squiggly red lines at them. Enter TypeScript Cookbook . With this practical guide, author Stefan Baumgartner provides senior engineers with solutions for everyday TypeScript problems.
If you're conversant with TypeScript as well as JavaScript basics, this book provides actionable recipes to help you tackle a wide array of issues. From setting up complex project structures to developing advanced helper types, each self-contained recipe guides you through the problem and discusses why and how a solution works.
The ideal companion for your ongoing TypeScript journey, this cookbook helps you:
- Dive into the inner workings of the TypeScript type system
- Integrate TypeScript into a variety of projects
- Craft advanced type definitions that allow for flexible scenarios
- Create useful helper types that function across projects
- Ensure readability along with type safety
- Create robust APIs for helper types and their coworkers
- Strongly type function signatures that rely on string types
- Work around limitations of the standard library
- Integrate TypeScript into advanced React projects
Publisher resources
Table of contents
- Foreword
- Preface
-
1. Project Setup
- 1.1. Type-Checking JavaScript
- 1.2. Installing TypeScript
- 1.3. Keeping Types on the Side
- 1.4. Migrating a Project to TypeScript
- 1.5. Loading Types from Definitely Typed
- 1.6. Setting Up a Full-Stack Project
- 1.7. Setting Up Tests
- 1.8. Typing ECMAScript Modules from URLs
- 1.9. Loading Different Module Types in Node
- 1.10. Working with Deno and Dependencies
- 1.11. Using Predefined Configurations
-
2. Basic Types
- 2.1. Annotating Effectively
- 2.2. Working with any and unknown
- 2.3. Choosing the Right Object Type
- 2.4. Working with Tuple Types
- 2.5. Understanding Interfaces Versus Type Aliases
- 2.6. Defining Function Overloads
- 2.7. Defining this Parameter Types
- 2.8. Working with Symbols
- 2.9. Understanding Value and Type Namespaces
-
3. The Type System
- 3.1. Modeling Data with Union and Intersection Types
- 3.2. Explicitly Defining Models with Discriminated Union Types
- 3.3. Exhaustiveness Checking with the Assert never Technique
- 3.4. Pinning Types with Const Context
- 3.5. Narrowing Types with Type Predicates
- 3.6. Understanding void
- 3.7. Dealing with Error Types in catch Clauses
- 3.8. Creating Exclusive Or Models with Optional never
- 3.9. Effectively Using Type Assertions
- 3.10. Using Index Signatures
- 3.11. Distinguishing Missing Properties and Undefined Values
- 3.12. Working with Enums
- 3.13. Defining Nominal Types in a Structural Type System
- 3.14. Enabling Loose Autocomplete for String Subsets
-
4. Generics
- 4.1. Generalizing Function Signatures
- 4.2. Creating Related Function Arguments
- 4.3. Getting Rid of any and unknown
- 4.4. Understanding Generic Instantiation
- 4.5. Generating New Object Types
- 4.6. Modifying Objects with Assertion Signatures
- 4.7. Mapping Types with Type Maps
- 4.8. Using ThisType to Define this in Objects
- 4.9. Adding Const Context to Generic Type Parameters
- 5. Conditional Types
- 6. String Template Literal Types
- 7. Variadic Tuple Types
- 8. Helper Types
-
9. The Standard Library
and External Type Definitions
- 9.1. Iterating over Objects with Object.keys
- 9.2. Explicitly Highlighting Unsafe Operations with Type Assertions and unknown
- 9.3. Working with defineProperty
- 9.4. Expanding Types for Array.prototype.includes
- 9.5. Filtering Nullish Values
- 9.6. Extending Modules
- 9.7. Augmenting Globals
- 9.8. Adding Non-JS Modules to the Module Graph
-
10. TypeScript and React
- 10.1. Writing Proxy Components
- 10.2. Writing Controlled Components
- 10.3. Typing Custom Hooks
- 10.4. Typing Generic forwardRef Components
- 10.5. Providing Types for the Context API
- 10.6. Typing Higher-Order Components
- 10.7. Typing Callbacks in React’s Synthetic Event System
- 10.8. Typing Polymorphic Components
-
11. Classes
- 11.1. Choosing the Right Visibility Modifier
- 11.2. Explicitly Defining Method Overrides
- 11.3. Describing Constructors and Prototypes
- 11.4. Using Generics in Classes
- 11.5. Deciding When to Use Classes or Namespaces
- 11.6. Writing Static Classes
- 11.7. Working with Strict Property Initialization
- 11.8. Working with this Types in Classes
- 11.9. Writing Decorators
-
12. Type Development Strategies
- 12.1. Writing Low Maintenance Types
- 12.2. Refining Types Step by Step
- 12.3. Checking Contracts with satisfies
- 12.4. Testing Complex Types
- 12.5. Validating Data Types at Runtime with Zod
- 12.6. Working Around Index Access Restrictions
- 12.7. Deciding Whether to Use Function Overloads or Conditional Types
- 12.8. Naming Generics
- 12.9. Prototyping on the TypeScript Playground
- 12.10. Providing Multiple Library Versions
- 12.11. Knowing When to Stop
- Index
- About the Author
Product information
- Title: TypeScript Cookbook
- Author(s):
- Release date: August 2023
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781098136659
You might also like
book
Learning TypeScript
TypeScript has conquered the world of JavaScript: it's one of the world's fastest growing and most …
video
Understanding TypeScript
Most people know TypeScript from Angular 2+. It's the language you must use there, right? It's …
book
Learn React with TypeScript - Second Edition
Take your first step in building modern-day component-based web apps using the latest features and capabilities …
book
Programming TypeScript
Any programmer working with a dynamically typed language will tell you how hard it is to …