Chapter 8. Helper Types
One of TypeScript’s strengths is the ability to derive types from other types. This allows you to define relationships between types, where updates in one type trickle through to all derived types automatically. This reduces maintenance and ultimately results in more robust type setups.
When creating derived types, we usually apply the same type modifications but in different combinations. TypeScript already has a set of built-in utility types, some of which we’ve already seen in this book. But sometimes they are not enough. Some situations require you either to apply known techniques differently or to dig deep into the inner workings of the type system to produce the desired result. You might need your own set of helper types.
This chapter introduces you to the concept of helper types and shows you some use cases where a custom helper type expands your ability to derive types from others tremendously. Each type is designed to work in different situations, and each type should teach you a new aspect of the type system. Of course, the list of types you see here is by no means complete, but they give you a good entry point and enough resources to branch out.
In the end, TypeScript’s type system can be seen as its own functional meta-programming language, where you combine small, single-purpose helper types with bigger helper types to make type derivates as easy as applying a single type to your existing models.
8.1 Setting Specific Properties Optional
Get TypeScript Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.