How to Omit values from an Enum in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 26, 2024
2 min

banner

# Omit values from an Enum in TypeScript

Use the Exclude utility type to omit values from an enum.

The Exclude utility type constructs a new type by excluding the provided members from the original type.

index.ts
// โœ… For Numeric Enums enum Sizes { Small, Medium, Large, ExtraLarge, } // ๐Ÿ‘‡๏ธ type WithoutSmall = Sizes.Medium | Sizes.Large | Sizes.ExtraLarge type WithoutSmall = Exclude<Sizes, Sizes.Small>; // ๐Ÿ‘‡๏ธ type WithoutMultiple = Sizes.Large | Sizes.ExtraLarge type WithoutMultiple = Exclude<Sizes, Sizes.Small | Sizes.Medium>;

omit values from enum in typescript

The code for this article is available on GitHub

We used the Exclude utility type to omit values from the enum.

The first type we passed to Exclude is the enum itself and the second is the values we want to exclude.

We could also directly pass the values without accessing them on the enum.

index.ts
enum Sizes { Small, Medium, Large, ExtraLarge, } // ๐Ÿ‘‡๏ธ type WithoutSmall = Sizes.Medium | Sizes.Large | Sizes.ExtraLarge type WithoutSmall = Exclude<Sizes, 0>;

The first member in a numeric enum without an initial value has a value of 0.

# Omit values from a String Enum in TypeScript

This approach would also work with a string enum.

index.ts
enum Sizes { Small = 'S', Medium = 'M', Large = 'L', ExtraLarge = 'XL', } // ๐Ÿ‘‡๏ธ type WithoutSmall = Sizes.Medium | Sizes.Large | Sizes.ExtraLarge type WithoutSmall = Exclude<Sizes, Sizes.Small>; // ๐Ÿ‘‡๏ธ type WithoutMultiple = Sizes.Large | Sizes.ExtraLarge type WithoutMultiple = Exclude<Sizes, Sizes.Small | Sizes.Medium>;
The code for this article is available on GitHub

You could also pass the values of the string enum directly, without referencing them on the enum.

index.ts
enum Sizes { Small = 'S', Medium = 'M', Large = 'L', ExtraLarge = 'XL', } // ๐Ÿ‘‡๏ธ type WithoutSmall = Sizes.Medium | Sizes.Large | Sizes.ExtraLarge type WithoutSmall = Exclude<Sizes, 'S'>; // ๐Ÿ‘‡๏ธ type WithoutMultiple = Sizes.Large | Sizes.ExtraLarge type WithoutMultiple = Exclude<Sizes, 'S' | 'M'>;

An easy way to visualize how the Exclude utility type works is to use a union type.

index.ts
type U = 'a' | 'b' | 'c'; // ๐Ÿ‘‡๏ธ type T1 = "a" | "b" type T1 = Exclude<U, 'c'>; // ๐Ÿ‘‡๏ธ type T2 = "a" type T2 = Exclude<U, 'c' | 'b'>;

The first type we passed to Exclude is the union type and the second is the types we want to omit for the constructed type.

If you need to exclude multiple types, make sure to separate them with a pipe |, and not a comma or a semicolon.

If you need to check if a value exists in an enum, click on the following article.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev