Hint
Infer the output type of a function, the input tuple, or the instance type of a class constructor
These utility types extract type information from function and class signatures.
ReturnType<T> — extracts the return type of a function type:
function fetchUser(id: number): Promise<User> { /* ... */ }
type FetchResult = ReturnType<typeof fetchUser>; // Promise<User>
type User2 = Awaited<ReturnType<typeof fetchUser>>; // User (unwrapped)
// Useful when the return type is complex and you don't want to duplicate it
const config = { theme: 'dark', lang: 'en', debug: false };
type Config = ReturnType<typeof createConfig>; // mirrors the function output
Parameters<T> — extracts the parameter types as a tuple:
function createUser(name: string, age: number, admin?: boolean): User { /* ... */ }
type CreateUserParams = Parameters<typeof createUser>;
// [name: string, age: number, admin?: boolean]
// Useful for wrapping or forwarding function calls
function withLogging<T extends (...args: any[]) => any>(
fn: T,
...args: Parameters<T>
): ReturnType<T> {
console.log('Calling with', args);
return fn(...args);
}
InstanceType<T> — extracts the instance type from a constructor:
class Connection {
host: string;
connect(): void { /* ... */ }
}
type ConnectionInstance = InstanceType<typeof Connection>; // Connection
// Useful in factory patterns where you work with constructors
function create<T extends new (...args: any[]) => any>(
Ctor: T, ...args: ConstructorParameters<T>
): InstanceType<T> {
return new Ctor(...args);
}
typeof to extract shapes from existing values — no need to manually define return types or parameter interfaces for functions you already have.