Typescript Foundations / Utility Types

Exclude

🚫 Exclude<T, U>

Exclude<T, U> creates a new union type by removing specific types from an existing union.

πŸ”Ή Example

type Role = "admin" | "user" | "guest";

type PublicRole = Exclude<Role, "admin">;

const role: PublicRole = "user"; // βœ…

Now PublicRole becomes:

"user" | "guest"

So this is not allowed:

const role: PublicRole = "admin"; // ❌ Error

πŸ’‘ Common Use

Exclude is useful when you have a union type but want to remove certain options from it.

Remember: Exclude<T, U> β†’ Removes U from the union T.