Lesson 41 of 55
15 mins readPython The infer Keyword in Conditional Types
In Plain English
The infer keyword allows you to introduce a new type variable within the true branch of a conditional type, automatically capturing return types, promise unwraps, or array elements.
Core Rules to Remember

infer Variable: Deduces types in-place within the condition extends clause.

Unwrapping: Commonly used to unwrap Promise<T> (Awaited) or extract function return types.
Live Interactive Example
Hit Run Code to see it liveUnwrapping Return Types and Promises
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Output Console
Click "Run Code" to view the rendered output.
How it works: infer R deduces the exact return type of the function and binds it to R.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Extract First Array Element Type
Define type First<T> = T extends [infer Head, ...any[]] ? Head : never.
Test with type F = First<[string, number, boolean]>.
Assign const item: F = "first_token" and print item.
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.