Menu

Post image 1
Post image 2
Post image 3
1 / 3
0

`infer` in Conditional Types: 5 Recipes That Aren't in the Handbook

DEV Community·Gabriel Anhaia·25 days ago
#oMx0MsXM
#recipe#typescript#types#generics#type#infer
Reading 0:00
15s threshold

Book: The TypeScript Type System — From Generics to DSL-Level Types Also by me: The TypeScript Library — the 5-book collection My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools Me: xgabriel.com | GitHub The handbook teaches infer with two examples. Awaited<T> peels a Promise . ReturnType<T> peels a function. Both are linear: one extends , one infer , one shape out. Real codebases ask infer to walk tuples or parse strings, and the working versions usually live in Stack Overflow answers older than the codebase you paste them into. Five recipes that go past the obvious. Each one starts with a real failure mode and ends with a lesson about how the type system handles distributivity, variance, and recursion depth. Recipe 1: Last element of a tuple The wrong version looks right at first glance. type Last < T extends readonly unknown [] > = T extends readonly [... unknown [], infer L ] ?…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More