avatarDr. Derek Austin 🥳

Summary

The web content provides an explanation and practical examples of TypeScript's Exclude utility type, demonstrating how it can be used to create a subset of a type by excluding specific values, such as defining a raccoon's diet by excluding vegetables.

Abstract

TypeScript's Exclude utility type is a powerful feature that enables developers to filter out unwanted types from a union type, effectively creating a new type that represents a more specific subset. The article illustrates this concept with a real-world analogy of a raccoon's diet, which includes fruits but not vegetables. By using Exclude, the author shows how to define a type that only includes the foods the raccoon would eat. The article also covers how to exclude multiple types simultaneously, further showcasing the versatility of Exclude. The conclusion emphasizes the utility of Exclude in TypeScript for creating precise and expressive types, contributing to the development of robust, type-safe applications.

Opinions

  • The author suggests that TypeScript's built-in utility types, such as Exclude, simplify developers' work by providing ready-made solutions for common type manipulation tasks.
  • The article implies that using Exclude can lead to more readable and maintainable code by allowing for clear and concise type definitions.
  • The author expresses a preference for union types over TypeScript enums, hinting at potential issues with enums and the superiority of union types in certain scenarios.
  • By providing a link to the TypeScript Playground, the author encourages hands-on experimentation with Exclude as an effective learning method.
  • The author highlights the extensive utility type ecosystem in TypeScript, mentioning other utility types like Partial, Readonly, Pick, and Omit, which collectively enhance the language's flexibility and power.

Exclude<UnionType, ExcludedMembers>

How To Use Exclude in TypeScript

A look at TypeScript’s built-in Exclude type, which is used when you need to make a subset of a type by excluding specific values.

Photo by Joshua J. Cotten on Unsplash

TypeScript has several built-in utility types to make the life of developers easier, and among them, Exclude is a particularly interesting one.

In the simplest terms, Exclude is a conditional type that allows you to create a new type by excluding specific types from a union type.

Sounds tricky? Don't worry. Once we dive into some practical examples, it will all make sense. And, we’ll be discussing the diet of mapaches!

What is the Exclude Utility Type?

The Exclude utility type is built into TypeScript. It’s used to create a new type by excluding some types from a union type, like a string union type.

Here’s the definition of Exclude:

type Exclude<T, U> = T extends U ? never : T

In this definition, T is a type variable that represents any type, and U is the type we want to exclude from T. If T extends U (meaning T is assignable to U), Exclude<T, U> is never; otherwise, it's T.

Let’s put this into practice with a real-world example.

Exclude in Action: No Vegetables for the Raccoon

Imagine we have a raccoon that is quite picky about its diet. It eats fruits, but it doesn’t like vegetables. We can represent this situation using TypeScript types and Exclude.

type Food = '🍎' | '🍊' | '🍇' | '🥦' | '🥕'
type Vegetable = '🥦' | '🥕'
type RaccoonDiet = Exclude<Food, Vegetable>

In this code, Food is a union type representing different kinds of food, and Vegetable is a type representing vegetables. We want to exclude vegetables from the food to get the diet for the raccoon. We use the Exclude utility type to achieve this. The RaccoonDiet type is now '🍎' | '🍊' | '🍇'.

With Exclude, we effectively created a new type that perfectly represents the diet of our picky raccoon. Isn’t that cool?

Excluding Multiple Types Simultaneously

The Exclude utility type can exclude multiple types at once. All you need to do is define the types you want to exclude as a union type. Let's extend our previous example.

type MoreFood = '🍎' | '🍊' | '🍇' | '🥦' | '🥕' | '🍞' | '🍪'
type NotRaccoonFood = '🥦' | '🥕' | '🍞'
type NewRaccoonDiet = Exclude<MoreFood, NotRaccoonFood>

In this code, we added bread (🍞) and cookies (🍪) to the Food type. Our raccoon doesn't eat bread, so we added it to the NotRaccoonFood type. The RaccoonDiet type is now '🍎' | '🍊' | '🍇' | '🍪'.

Conclusion: TypeScript’s Built-In Exclude Type

The Exclude utility type in TypeScript is a handy tool that allows you to create a new type by excluding certain types from a union type. It is particularly useful when you need to represent a subset of a type that excludes specific values. You might even call it an “exclusive” type. 😆

In this article, we looked at how Exclude works and how to use it in practical examples. We saw how it can be used to model a raccoon’s diet by excluding certain types of food. With Exclude, we can create more precise and expressive types that better capture the requirements of our programs.

It’s worth noting that Exclude is just one of TypeScript’s many utility types. Others include Partial, Readonly, Pick, Omit, and many more. These utility types are part of what makes TypeScript such a powerful and flexible language for developing robust, type-safe applications.

As always, the best way to learn is by doing. So don’t hesitate to experiment with Exclude and other TypeScript utility types in your own projects. Better yet, review today’s code examples in the TypeScript Playground or read the TypeScript docs on Excluders>.

Happy coding! 🦝🍎🍊🍇🍪

Photo by Unsplash+
Typescript
Programming
JavaScript
Software Development
Web Development
Recommended from ReadMedium