Skip to content
dreamcode
dreamcode
Map
Conditional
Lesson 20 of 26
+15 XP on finish
TYPESCRIPTChapter 4 · TS Expert

Conditional Types

Conditional Types let you select types dynamically by checking if type T extends type U. They work like ternary conditional operators (T extends U ? X : Y).

Worked example

How it reads

  • T extends string tests if the generic type extends a string
  • ? 'yes' : 'no' resolves to different types depending on the check
Cloud tip: Conditional types are the core of advanced type manipulation and libraries.
index.ts
TYPESCRIPT
type-checked, then run in your browser
Console
Run your code to see its output here.
YOUR TURN

Write type IsArray<T> = T extends unknown[] ? "array" : "single", declare const kind: IsArray<number[]> = "array", and log it.

Press Run to check your work.