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
Author(s): Stefan Baumgartner
Edition: 1
Publisher: O'Reilly Media
Year: 2023
Language: English
Commentary: Publisher's PDF
Pages: 419
City: Sebastopol, CA
Tags: JavaScript; Cookbook; TypeScript; Standard Library; Type-Driven Development; React.js; Type Systems
Cover
Copyright
Table of Contents
Foreword
Preface
Who This Book Is For
What’s in This Book?
Organization of This Book
Conventions Used in This Book
Programming Conventions
Typesetting Conventions
Using Code Examples
O’Reilly Online Learning
How to Contact Us
Acknowledgments
Chapter 1. Project Setup
1.1 Type-Checking JavaScript
Problem
Solution
Discussion
1.2 Installing TypeScript
Problem
Solution
Discussion
1.3 Keeping Types on the Side
Problem
Solution
Discussion
1.4 Migrating a Project to TypeScript
Problem
Solution
Discussion
1.5 Loading Types from Definitely Typed
Problem
Solution
Discussion
1.6 Setting Up a Full-Stack Project
Problem
Solution
Discussion
1.7 Setting Up Tests
Problem
Solution
Discussion
1.8 Typing ECMAScript Modules from URLs
Problem
Solution
Discussion
1.9 Loading Different Module Types in Node
Problem
Solution
Discussion
1.10 Working with Deno and Dependencies
Problem
Solution
Discussion
1.11 Using Predefined Configurations
Problem
Solution
Discussion
Chapter 2. Basic Types
2.1 Annotating Effectively
Problem
Solution
Discussion
2.2 Working with any and unknown
Problem
Solution
Discussion
2.3 Choosing the Right Object Type
Problem
Solution
Discussion
2.4 Working with Tuple Types
Problem
Solution
Discussion
2.5 Understanding Interfaces Versus Type Aliases
Problem
Solution
Discussion
2.6 Defining Function Overloads
Problem
Solution
Discussion
2.7 Defining this Parameter Types
Problem
Solution
Discussion
2.8 Working with Symbols
Problem
Solution
Discussion
2.9 Understanding Value and Type Namespaces
Problem
Solution
Discussion
Chapter 3. The Type System
3.1 Modeling Data with Union and Intersection Types
Problem
Solution
Discussion
3.2 Explicitly Defining Models with Discriminated Union Types
Problem
Solution
Discussion
3.3 Exhaustiveness Checking with the Assert never Technique
Problem
Solution
Discussion
3.4 Pinning Types with Const Context
Problem
Solution
Discussion
3.5 Narrowing Types with Type Predicates
Problem
Solution
Discussion
3.6 Understanding void
Problem
Solution
Discussion
3.7 Dealing with Error Types in catch Clauses
Problem
Solution
Discussion
3.8 Creating Exclusive Or Models with Optional never
Problem
Solution
Discussion
3.9 Effectively Using Type Assertions
Problem
Solution
Discussion
3.10 Using Index Signatures
Problem
Solution
Discussion
3.11 Distinguishing Missing Properties and Undefined Values
Problem
Solution
Discussion
3.12 Working with Enums
Problem
Solution
Discussion
3.13 Defining Nominal Types in a Structural Type System
Problem
Solution
Discussion
3.14 Enabling Loose Autocomplete for String Subsets
Problem
Solution
Discussion
Chapter 4. Generics
4.1 Generalizing Function Signatures
Problem
Solution
Discussion
4.2 Creating Related Function Arguments
Problem
Solution
Discussion
4.3 Getting Rid of any and unknown
Problem
Solution
Discussion
4.4 Understanding Generic Instantiation
Problem
Solution
Discussion
4.5 Generating New Object Types
Problem
Solution
Discussion
4.6 Modifying Objects with Assertion Signatures
Problem
Solution
Discussion
4.7 Mapping Types with Type Maps
Problem
Solution
Discussion
4.8 Using ThisType to Define this in Objects
Problem
Solution
Discussion
4.9 Adding Const Context to Generic Type Parameters
Problem
Solution
Discussion
Chapter 5. Conditional Types
5.1 Managing Complex Function Signatures
Problem
Solution
Discussion
5.2 Filtering with never
Problem
Solution
Discussion
5.3 Grouping Elements by Kind
Problem
Solution
Discussion
5.4 Removing Specific Object Properties
Problem
Solution
Discussion
5.5 Inferring Types in Conditionals
Problem
Solution
Discussion
Chapter 6. String Template Literal Types
6.1 Defining a Custom Event System
Problem
Solution
Discussion
6.2 Creating Event Callbacks with String Manipulation Types and Key Remapping
Problem
Solution
Discussion
6.3 Writing a Formatter Function
Problem
Solution
Discussion
6.4 Extracting Format Parameter Types
Problem
Solution
Discussion
6.5 Dealing with Recursion Limits
Problem
Solution
Discussion
6.6 Using Template Literals as Discriminants
Problem
Solution
Discussion
Chapter 7. Variadic Tuple Types
7.1 Typing a concat Function
Problem
Solution
Discussion
7.2 Typing a promisify Function
Problem
Solution
Discussion
7.3 Typing a curry Function
Problem
Solution
Discussion
7.4 Typing a Flexible curry Function
Problem
Solution
Discussion
7.5 Typing the Simplest curry function
Problem
Solution
Discussion
7.6 Creating an Enum from a Tuple
Problem
Solution
Discussion
7.7 Splitting All Elements of a Function Signature
Problem
Solution
Discussion
Chapter 8. Helper Types
8.1 Setting Specific Properties Optional
Problem
Solution
Discussion
8.2 Modifying Nested Objects
Problem
Solution
Discussion
8.3 Remapping Types
Problem
Solution
Discussion
8.4 Getting All Required Keys
Problem
Solution
Discussion
8.5 Allowing at Least One Property
Problem
Solution
Discussion
8.6 Allowing Exactly One and All or None
Problem
Solution
Discussion
8.7 Converting Union to Intersection Types
Problem
Solution
Discussion
8.8 Using type-fest
Problem
Solution
Discussion
Chapter 9. The Standard Library and External Type Definitions
9.1 Iterating over Objects with Object.keys
Problem
Solution
Discussion
9.2 Explicitly Highlighting Unsafe Operations with Type Assertions and unknown
Problem
Solution
Discussion
9.3 Working with defineProperty
Problem
Solution
Discussion
9.4 Expanding Types for Array.prototype.includes
Problem
Solution
Discussion
9.5 Filtering Nullish Values
Problem
Solution
Discussion
9.6 Extending Modules
Problem
Solution
Discussion
9.7 Augmenting Globals
Problem
Solution
Discussion
9.8 Adding Non-JS Modules to the Module Graph
Problem
Solution
Discussion
Chapter 10. TypeScript and React
10.1 Writing Proxy Components
Problem
Solution
Discussion
10.2 Writing Controlled Components
Problem
Solution
Discussion
10.3 Typing Custom Hooks
Problem
Solution
Discussion
10.4 Typing Generic forwardRef Components
Problem
Solution
Discussion
10.5 Providing Types for the Context API
Problem
Solution
Discussion
10.6 Typing Higher-Order Components
Problem
Solution
Discussion
10.7 Typing Callbacks in React’s Synthetic Event System
Problem
Solution
Discussion
10.8 Typing Polymorphic Components
Problem
Solution
Discussion
Chapter 11. Classes
11.1 Choosing the Right Visibility Modifier
Problem
Solution
Discussion
11.2 Explicitly Defining Method Overrides
Problem
Solution
Discussion
11.3 Describing Constructors and Prototypes
Problem
Solution
Discussion
11.4 Using Generics in Classes
Problem
Solution
Discussion
11.5 Deciding When to Use Classes or Namespaces
Problem
Solution
Discussion
11.6 Writing Static Classes
Problem
Solution
Discussion
11.7 Working with Strict Property Initialization
Problem
Solution
Discussion
11.8 Working with this Types in Classes
Problem
Solution
Discussion
11.9 Writing Decorators
Problem
Solution
Discussion
Chapter 12. Type Development Strategies
12.1 Writing Low Maintenance Types
Problem
Solution
Discussion
12.2 Refining Types Step by Step
Problem
Solution
Discussion
12.3 Checking Contracts with satisfies
Problem
Solution
Discussion
12.4 Testing Complex Types
Problem
Solution
Discussion
12.5 Validating Data Types at Runtime with Zod
Problem
Solution
Discussion
12.6 Working Around Index Access Restrictions
Problem
Solution
Discussion
12.7 Deciding Whether to Use Function Overloads or Conditional Types
Problem
Solution
Discussion
12.8 Naming Generics
Problem
Solution
Discussion
12.9 Prototyping on the TypeScript Playground
Problem
Solution
Discussion
12.10 Providing Multiple Library Versions
Problem
Solution
Discussion
12.11 Knowing When to Stop
Problem
Solution
Discussion
Index
About the Author