TypeScript · Middle · Question 7 of 75

What does infer do in type ElementType<T> = T extends (infer U)[] ? U : never?

Explanation

infer is used only in the extends part of a conditional type: TS matches T against the pattern and "captures" the needed part. ElementType<string[]> → string. That's how ReturnType, Parameters and Awaited work.

Further reading

Problem with a question

TypeScript · Middle · question 7

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz