The infer Keyword
Why This Is Asked
infer is an advanced conditional type feature that separates TypeScript power users from casual users. Interviewers use this to assess whether you can build utility types, understand conditional type mechanics, and read complex generic signatures in library code.
Key Concepts
inferdeclares a type variable within theextendsclause of a conditional type, capturing a sub-type- The inferred variable is only available in the
truebranch of the conditional - Powers built-in utility types:
ReturnType<T>,Parameters<T>,Awaited<T>,InstanceType<T> - Can extract types from any position: function return types, promise inner types, array element types, tuple positions
- Advanced: multiple
inferpositions, recursive conditional types, template literal type extraction