The infer keyword lets TypeScript guess a type inside a conditional type. It helps extract or infer a type from another type.
Click to reveal answer
intermediate
How do you use infer inside a conditional type?
You write a conditional type with extends and inside it use infer T to capture a type part. For example: type GetReturn<T> = T extends (...args: any[]) => infer R ? R : never;
Click to reveal answer
beginner
What happens if the type does not match the pattern when using infer?
If the type does not match the pattern in the conditional type, the infer part is ignored and the false branch of the conditional type is used.
Click to reveal answer
intermediate
Give an example of inferring the argument type of a function using infer.
Example: type ArgType<T> = T extends (arg: infer A) => any ? A : never; This extracts the type of the first argument of a function type T.
Click to reveal answer
beginner
Why is infer useful in TypeScript?
infer helps create flexible and reusable types by extracting parts of complex types automatically. It reduces manual type writing and improves type safety.
Click to reveal answer
What does the infer keyword do in TypeScript conditional types?
AExtracts a type from another type
BDeclares a new variable
CCreates a new interface
DDefines a function
✗ Incorrect
infer is used to extract or guess a type inside conditional types.
Which syntax correctly uses infer to get a function's return type?
Atype R<T> = T => infer R;
Btype R<T> = T extends (...args: any[]) => infer R ? R : never;
Ctype R<T> = T extends infer R ? R : never;
Dtype R<T> = infer R extends T ? R : never;
✗ Incorrect
Option B correctly uses infer inside a conditional type to extract the return type.
What happens if the type does not match the pattern when using infer?
AThe program crashes
BTypeScript throws an error
CThe inferred type becomes <code>any</code>
DThe <code>infer</code> type is ignored and the false branch is used
✗ Incorrect
If the pattern does not match, the conditional type uses the false branch, ignoring infer.
Which of these extracts the first argument type of a function using infer?
Atype Arg<T> = infer A extends T ? A : never;
Btype Arg<T> = T extends infer A => any ? A : never;
Ctype Arg<T> = T extends (arg: infer A) => any ? A : never;
Dtype Arg<T> = T => infer A;
✗ Incorrect
Option C correctly infers the first argument type of a function type.
Why is infer useful in TypeScript?
AIt helps extract types automatically for flexible type definitions
BIt runs code at runtime
CIt creates new variables
DIt replaces interfaces
✗ Incorrect
infer helps create reusable and safe types by extracting parts of types automatically.
Explain how the infer keyword works inside a TypeScript conditional type.
Think about how TypeScript guesses a type inside a condition.
You got /4 concepts.
Give an example of using infer to extract the return type of a function type.
Use a conditional type with <code>extends</code> and <code>infer R</code>.
You got /4 concepts.
Practice
(1/5)
1. What does the infer keyword do in TypeScript conditional types?
easy
A. It declares a new variable inside a function.
B. It allows TypeScript to guess and extract a type inside a conditional type.
C. It creates a new interface automatically.
D. It imports types from external modules.
Solution
Step 1: Understand the role of infer
The infer keyword is used inside conditional types to let TypeScript guess a type variable from a given type.
Step 2: Identify what infer extracts
It extracts parts of complex types, like the return type of a function or the element type of an array.
Final Answer:
It allows TypeScript to guess and extract a type inside a conditional type. -> Option B
2. Which of the following is the correct syntax to infer the return type of a function type F using infer?
easy
A. type ReturnType<F> = F extends (...args: any[]) => infer R ? R : never;
B. type ReturnType<F> = infer R extends F ? R : never;
C. type ReturnType<F> = F extends infer R => any ? R : never;
D. type ReturnType<F> = F extends (...args: infer R) => any ? R : never;
Solution
Step 1: Analyze the conditional type structure
The correct syntax uses F extends (...args: any[]) => infer R to extract the return type R.
Step 2: Check each option for correct infer usage
type ReturnType<F> = F extends (...args: any[]) => infer R ? R : never; correctly places infer R after the arrow function parameters and before the return type.
Final Answer:
type ReturnType<F> = F extends (...args: any[]) => infer R ? R : never; -> Option A
Quick Check:
Correct infer syntax for return type = A [OK]
Hint: Look for infer R right after function args in extends [OK]
Common Mistakes:
Placing infer outside the extends clause
Using infer on the wrong part of the function type
Confusing parameter inference with return type inference
3. Given the following code, what is the type of ElementType?
type ElementType<T> = T extends (infer U)[] ? U : T;
type Result = ElementType;
medium
A. string
B. unknown
C. never
D. string[]
Solution
Step 1: Understand the conditional type with infer
The type checks if T extends an array of some type U. If yes, it extracts U.
Step 2: Apply the type to string[]
Since string[] extends (infer U)[], U is string. So, ElementType<string[]> is string.
Final Answer:
string -> Option A
Quick Check:
Extracted element type = string [OK]
Hint: infer extracts array element type inside conditional [OK]
Common Mistakes:
Confusing the whole array type with element type
Assuming infer returns the original type
Ignoring the conditional fallback
4. Identify the error in this type definition that tries to infer the argument type of a function:
type ArgType<T> = T extends (arg: infer A) => void ? A : never;
type Test = ArgType<() => number>;
medium
A. The conditional type should check for return type, not argument type.
B. The syntax of infer is incorrect; it should be outside the parentheses.
C. The function type has no arguments, so infer fails and returns never.
D. The type alias is missing a generic constraint.
Solution
Step 1: Analyze the function type used
The function type () => number has no arguments.
Step 2: Understand infer behavior with no matching argument
The conditional type tries to infer argument A, but since there is no argument, the condition fails and returns never.
Final Answer:
The function type has no arguments, so infer fails and returns never. -> Option C
Quick Check:
Infer fails with no matching argument = C [OK]
Hint: Check if function actually has arguments before inferring [OK]
Common Mistakes:
Assuming infer always succeeds
Misplacing infer keyword syntax
Confusing argument and return type inference
5. You want to create a type Flatten<T> that extracts the element type from nested arrays using infer. For example, Flatten<number[][]> should be number[]. Which of the following correctly implements this?
hard
A. type Flatten<T> = T extends (infer U)[] ? U[] : T;
B. type Flatten<T> = T extends (infer U)[] ? Flatten : T;
C. type Flatten<T> = T extends any[] ? Flatten : T;
D. type Flatten<T> = T extends (infer U)[] ? U extends any[] ? Flatten<U> : U[] : T;
Solution
Step 1: Understand the goal of Flatten
We want to recursively unwrap nested arrays until we reach a non-array element, then wrap it back as a single array.
Step 2: Analyze type Flatten<T> = T extends (infer U)[] ? U extends any[] ? Flatten<U> : U[] : T;'s recursive conditional
type Flatten<T> = T extends (infer U)[] ? U extends any[] ? Flatten<U> : U[] : T; checks if T is an array of U. If U is itself an array, it recursively calls Flatten<U>. Otherwise, it returns U[]. This matches the requirement.
Final Answer:
type Flatten<T> = T extends (infer U)[] ? U extends any[] ? Flatten<U> : U[] : T; -> Option D
Quick Check:
Recursive infer for nested arrays = B [OK]
Hint: Use recursive conditional with infer to unwrap nested arrays [OK]
Common Mistakes:
Not handling recursion for nested arrays
Returning wrong type shape after unwrapping
Misplacing infer or conditional checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.