TypeScript Exclude: How to use Exclude in TypeScript?

What is TypeScript Exclude?

The Exclude utility type in TypeScript is used to create a type by excluding certain types from another type.

It’s like filtering out some types from a given type.

Here’s the syntax for Exclude:

type Exclude = T extends U ? never : T;

In this syntax, T is the original type, and U is the type you want to exclude from T. The extends keyword is used to check if T is assignable to U.

If it is, never is returned, effectively excluding it. If not, T is returned.

Let’s see an example:

type T1 = Exclude<"x" | "y" | "z", "x">;
// Result: "y" | "z"

In this example, the type "x" is excluded from the union type "x" | "y" | "z", resulting in the type "y" | "z".

Moreover, the Exclude utility type is used to construct a type by excluding certain types from a union. It allows developers to remove specific types from a union type, making it easier to work with subsets of types.

The Exclude type is particularly useful when dealing with large and complex type unions, as it provides a way to simplify type definitions and ensure type safety.

How to use Exclude in TypeScript?

Here’s the step-by-step guide in order to use Exclude in TypeScript.

Step 1:

Define your original type.This could be an object type representing an employee:

type Employee = {
name: string;
age: number;
position: string;
salary: number;
};

Step 2:

Decide which properties you want to exclude from your original type. In this case, let’s say we want to exclude salary.

Step 3:

Use the Exclude utility type to create a new type that excludes the specified properties from the original type.

Here’s how you do it:

type PublicEmployee = {
};

Now, PublicEmployee is a type that includes name, age, and position, but not salary.

Step 4:

You can now use PublicEmployee in your code just like any other type.

For example, you can create a function that takes a PublicEmployee as an argument:

function printEmployeeDetails(employee: PublicEmployee) {
console.log(Name: ${employee.name});
console.log(Age: ${employee.age});
console.log(Position: ${employee.position});
}

This function will print the details of an employee, excluding the salary.

10 Ways to Use Exclude in TypeScript with Examples

Here are the 10 ways to use Exclude in TypeScript with examples.

  1. Exclude Specific Types from Union Types:
type SampleSubject = "Programming" | "Software Development" | "Web Development";
type NewType = Exclude<SampleSubject, "Programming" | "Software Development">; 

// Result: "Web Development"

  1. Exclude Number Types from Union Types:
type MixedType = string | number | boolean;
type NoNumbers = Exclude; // Result: string | boolean

  1. Exclude Specific Object Properties:
type Employee = {
name: string;
age: number;
salary: number;
};
type PublicEmployee = {
}; // Result: { name: string; age: number; }

  1. Exclude Null and Undefined Types:
type NullableType = string | null | undefined;
type NonNullableType = Exclude; // Result: string

  1. Exclude Specific Enum Members:
enum Colors {
Red,
Green,
Blue
}
type PrimaryColors = Exclude; // Result: Colors.Red | Colors.Blue

  1. Exclude Specific Classes:
class Circle { /* … / } class Square { / … */ }
type Shape = Circle | Square;
type NonSquareShapes = Exclude; // Result: Circle

  1. Exclude Types from Any:
type NotBoolean = Exclude; // Result: any except boolean

  1. Exclude Types from Unknown:
type NotNumber = Exclude; // Result: unknown except number

  1. Exclude Function Types:
type MixedType = string | (() => void);
type NoFunctions = Exclude void>; // Result: string

  1. Exclude Array Types:
type MixedType = string | string[];
type NoArrays = Exclude; // Result: string

Those are the 10 different ways to use Exclude in TypeScript.
The examples show how the Exclude utility type can be used to create new types by excluding specific types from existing ones.

Conclusion

Finally, we’re done discussing the Exclude utility type in TypeScript, which is used to create new types by excluding specific types from existing ones.

It’s like filtering out some types from a given type, which can simplify type definitions and ensure type safety, especially when dealing with large and complex type unions.

We’ve also explored the different ways to use the Exclude utility type in TypeScript, along with examples.

These examples demonstrate the versatility and usefulness of the Exclude utility type in TypeScript.

By understanding and utilizing this utility type, developers can write more efficient and safer TypeScript code.

I hope this article has given you some insights and helped you understand the TypeScript Record.

If you have any questions or inquiries, please don’t hesitate to leave a comment below.

Leave a Comment