Command Palette

Search for a command to run...

Hectal
Phase 9Advanced10 of 18 in TypeScript

Conditional and Template Literal Types

Types that make decisions: conditional types, distribution over unions, infer, template literal types and recursive types, built one small step at a time.

Until now, you have written types by hand. In this phase, types start to compute. A conditional type asks a yes/no question about a type and answers with a different type. **infer pulls a piece out of a type. Template literal types** build text types like onClick from smaller pieces. Recursive types let a type refer to itself, so it can describe a tree of any depth.

This is the part of TypeScript that library authors use to make ReturnType, Awaited, typed event names and typed route parameters work. It looks scary at first, so every type here is built step by step, and you will check each step with // ^? in the live editor. Nothing is guessed: if you can read what the Types panel says, you can understand the type.

Be careful too. Clever types are code that other people must read, and the compiler has hard limits on how deep they may go. Topic 9.5 shows the limits with real error messages, and Topic 10.6 comes back to when to stop.

0/5 · 0%
5 topics ~2 h 16 code blocks & diagrams
Start with the first topic

Practise this phase

Hands-on exercises: write the code, see your page live, and press Check to have it tested automatically.