Lesson 45 of 55
15 mins readPython Exclude, Extract, and NonNullable
In Plain English
While Pick and Omit operate on object property keys, Exclude and Extract operate on union members directly. NonNullable removes null and undefined.
Core Rules to Remember

Exclude<Union, Members>: Strips matching members from a union.

Extract<Union, Members>: Extracts matching members from a union.
Live Interactive Example
Hit Run Code to see it liveFiltering Unions
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: Exclude strips matching types; Extract retains only matching types.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Filter Event Types
Define type AllEvents = "click" | "hover" | "focus" | "scroll".
Create type MouseEvents = Extract<AllEvents, "click" | "hover">.
Create const e: MouseEvents = "click" and print `Handled ${e}`.
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.