Skip to content

Implement Exclude

Given a tuple, you need create a generic Exclude, pick the length of the tuple.

Exclude is a utility type that removes types from a union type.

For example:

type Result = Exclude<string | number | boolean, number>; // string | boolean

Solution

type MyExclude<T, U> = T extends U ? never : T;

Example of Usage:

type Result = MyExclude<string | number | boolean, number>; // string | boolean

This solution works because the extends keyword in a conditional type checks for assignability, and never is the type that represents the bottom type, which is returned when the condition is not met.

References