TS EXPERTChapter 4 · TS Expert
Inferring Types with infer
Inside a conditional type, infer declares a type variable that TypeScript fills in from the match. T extends Promise<infer V> ? V : T extracts what a promise resolves to, and F extends (...args: any[]) => infer R ? R : never extracts a function's return type. The built-in helpers ReturnType and Awaited are written exactly this way.
Worked example
How it reads
infer Vcaptures whatever sits insidePromise<...>Result<typeof makeStar>derives the Star type from the function itself- When the pattern does not match, the false branch is used

Cloud tip: Reach for
infer when you want a type that is buried inside another one; it keeps derived types in sync with their source.

