Lesson 40 of 55
15 mins readPython Conditional Types
In Plain English
At the heart of modern TypeScript type metaprogramming lies the conditional type. It selects one of two possible types based on a condition expressed as a type relationship test.
Core Rules to Remember

Ternary Logic: T extends U ? X : Y resolves types based on subtype relations.

Distributive Behavior: Automatically distributes over union types when naked type parameters are tested.
Live Interactive Example
Hit Run Code to see it liveTernary Type Branching
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: T extends string ? "YES_STRING" : "NO_STRING" evaluates at compile time based on the passed type argument.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Create an Array Element Resolver
Define conditional type Flatten<T> = T extends any[] ? T[number] : T.
Test with type F1 = Flatten<string[]> and type F2 = Flatten<number>.
Assign const v1: F1 = "zen" and const v2: F2 = 42. Print `${v1} ${v2}`.
1
2
3
4
5
6
7
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.