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;
T extends U
: This checks if the type T is assignable to the type U.T extends U ? never : T
: This is a conditional type that returnsnever
if T is assignable to U, otherwise returns 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.