TypeScript is one of the most important tools for jаvascript 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 jаvascript 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.
You want to get started with TypeScript, fantastic! The big question is: How do you start? There are many ways how you can integrate TypeScript into your projects, and they all are slightly different depending on your project’s needs. Just as jаvascript runs on many runtimes, there are plenty of ways to configure TypeScript so it meets your target’s needs.
According to the 2022 State of jаvascript survey, almost 70% of all participants actively use TypeScript. The 2022 StackOverflow survey lists TypeScript as one of the top 5 most popular languages and the fourth highest in user satisfaction. At the beginning of 2023, TypeScript counts more than 40 million weekly downloads on NPM. There is no doubt: TypeScript is a phenomenon!
The Chapter 1 covers all the possibilities of introducing TypeScript to your project, as an extension next to jаvascript that gives you basic auto-completion and error indication, up to full-fledge setups for full-stack applications on Node.js and the browser. Since jаvascript tooling is a field with endless possibilities — some say that a new jаvascript build chain is released every week, almost as much as new frameworks — this chapter focuses more on things you can do with the TypeScript compiler alone and without any extra tool. TypeScript offers everything you need for your transpilation needs, except the ability to create minified and optimized bundles for web distribution. Bundlers like ESBuild or Webpack take care of this task. Also, there are setups that include other transpilers like Babel.js which can play along nicely with TypeScript.
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
Publisher: O'Reilly Media, Inc.
Year: 2023
Language: English
Pages: 768
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 Pre-defined 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 vs 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. Distinguish 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
5. Conditional Types
5.1. Managing Complex Function Signatures
5.2. Filtering with never
5.3. Grouping Elements by Kind
5.4. Removing Specific Object Properties
5.5. Inferring Types in Conditionals
6. String Template Literal Types
6.1. Defining a Custom Event System
6.2. Creating Event Callbacks with String Manipulation Types and Key Re-Mapping
6.3. Writing a Formatter Function
6.4. Extracting Format Parameter Types
6.5. Dealing with Recursion Limits
6.6. Using Template Literals as Discriminants
About the Author