Implement TypeScript's built-in Exclude utility type from scratch. Master conditional types and type distribution in this easy-level challenge on TypeScriptPro.
Filter out unwanted types from unions with the Exclude utility! 🚫
In this easy-level challenge, you'll implement the built-in Exclude<T, U>
utility type without using it directly. Exclude removes from union type T
all types that are assignable to U
. This is one of TypeScript's most fundamental utility types and understanding how it works will deepen your knowledge of conditional types and distributive behavior.
You'll learn how conditional types distribute over union types, a powerful feature that enables many advanced type manipulations in TypeScript.
For this challenge, you will need to change the following code to make the tests pass (no type check errors).
Implement the built-in Exclude<T, U>
Exclude from
T
those types that are assignable toU
For example:
type Result = MyExclude<'a' | 'b' | 'c', 'a'> // 'b' | 'c'
The Exclude utility leverages TypeScript's distributive conditional types to filter union members.
type MyExclude<T, U> = T extends U ? never : T
How it works:
never
, otherwise return the membernever
in a union disappears, effectively filtering out those typesExample breakdown for MyExclude<'a' | 'b' | 'c', 'a'>
:
('a' extends 'a' ? never : 'a') | ('b' extends 'a' ? never : 'b') | ('c' extends 'a' ? never : 'c')
never | 'b' | 'c'
'b' | 'c'
This pattern is fundamental to many type utilities in TypeScript.
Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.
Only 27 Spots left
Get 1 month early access
Pre-Launch discount
This challenge is originally from here.