Q19. What does infer do?
infer declares a type variable within a conditional type pattern so TypeScript can capture a matching part of a type. It can extract promise values, function return types, or array element types.
In T extends Promise<infer U> ? U : T, U captures the promise's resolved type.
Note
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
TypeScript concept
// Illustrative TypeScript contract
type Identifier = string;
const id: Identifier = "item-1";💡 The example demonstrates the compile-time contract discussed in this question.
Key Takeaways
- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
Knowledge Check
Does `infer` create a JavaScript variable at runtime?