infer
A keyword used inside the extends part of a conditional type to capture part of a matched type in a new type variable.
In more detail
In T extends (infer E)[] ? E : T, E becomes the element type when T is an array. It only works in conditional types, and is how ReturnType, Parameters and Awaited are built.
Related terms
- conditional type — A type that picks one of two types depending on whether a type is assignable to another, written like a ternary expression with extends.
- pattern matching — Testing a value against a shape, such as a type, a range or the values of its properties, and optionally extracting parts of it in the same step.
- ReturnType<T> — The utility type that gives the return type of a function type T.
Taught in
- Type Annotations, Type Inference And Contextual Typing — Production TypeScript
- The in Operator And Instanceof Narrowing — Production TypeScript
- Custom Type Predicates: Functions Returning is Type — Production TypeScript
- Why Generics: Functions, Identities And Type Variables — Production TypeScript
- Default Generic Arguments And Multi-Type Parameters — Production TypeScript
- Const Type Parameters: Retaining Literal Types In Functions — Production TypeScript
- Function Utilities: ReturnType, Parameters And Constructor — Production TypeScript
- Conditional Types: Ternary Types And Type Inference With infer — Production TypeScript
- Mapped Types, Key Remapping With as And Template Literals — Production TypeScript