infer. What is the type of Result?type ExtractReturnType<T> = T extends (...args: any[]) => infer R ? R : never; type Func = (x: number) => string; type Result = ExtractReturnType<Func>; // What is the type of Result?
infer R captures inside the function type.The infer R keyword extracts the return type of the function type T. Since Func returns a string, Result is string.
infer, what is the type of First?type FirstElement<T> = T extends [infer U, ...any[]] ? U : never; type Tuple = [boolean, number, string]; type First = FirstElement<Tuple>; // What is the type of First?
infer U captures the first element of the tuple.The type FirstElement extracts the first element type from a tuple. Since Tuple starts with boolean, First is boolean.
type ArgType<T> = T extends (arg: infer A, arg2: infer B) => any ? A : never; type Fn = (x: number, y: string) => void; type Result = ArgType<Fn>; // Why does this cause an error?
TypeScript does not allow multiple infer variables in a single function parameter list like this. The syntax is invalid and causes a syntax error.
InnerType after this nested inference?type NestedReturnType<T> = T extends () => () => infer R ? R : never; type Fn = () => () => number; type InnerType = NestedReturnType<Fn>; // What is InnerType?
The type NestedReturnType extracts the return type of the inner function. Since Fn returns a function that returns number, InnerType is number.
infer. Which option correctly does this?Option B correctly uses Array<infer U> to extract the element type. Option B is invalid syntax. Option B only matches single-element tuples. Option B infers length, not element type.
