🔷 TypeScript Intermediate

What is the ReturnType utility type?

Answer

ReturnType<T> is a built-in utility type that extracts the return type of a function type T. It is implemented using conditional types and infer: type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any. Usage: function getUserName(): string { return "Alice"; } type NameType = ReturnType<typeof getUserName>;NameType is string. This is powerful when you want to derive types from existing functions rather than writing them manually — the type is always in sync with the function. For async functions, it returns Promise<T>; combine with Awaited to unwrap: type Resolved = Awaited<ReturnType<typeof asyncFn>>. ReturnType is particularly useful for functions that return complex objects — infer the type from the function rather than duplicating the type definition.