Web Developer

|

Stockholm, Sweden

Delivering scalable solutions for
startups, enterprises, and agencies.

ReturnType in TypeScript

What is ReturnType?

ReturnType is a TypeScript utility that extracts the return type from any function. Instead of manually defining what a function returns, it automatically captures and reuses that type.

shape shifter

How it works

Point ReturnType at a function, and it gives you back the exact type that function returns. No guessing, no manual typing - it reads the function and mirrors its output type.

Say you have fetchUser() that returns { id: 1, name: "John", email: "john@example.com" }. Use type User = ReturnType<typeof fetchUser> and now User automatically becomes { id: number; name: string; email: string }.

Why use ReturnType?

  • Auto-sync - When your function's return type changes, dependent types update automatically
  • Single source of truth - The function defines the type, everything else follows
  • Less maintenance - No need to manually update multiple type definitions

Real-world scenario

Your API function returns user data with id, name, and email. Later you add avatar field. With ReturnType, every type that depends on this function automatically includes the new avatar field without you touching a single type definition.

When the function evolves

Add a new field to your function's return? Every type using ReturnType automatically includes it. No manual updates needed.

Key Benefits

  • Stays in sync - Types automatically reflect function changes
  • Reduces errors - No mismatched types from manual definitions
  • Saves time - Write the logic once, let TypeScript handle the types
Web Developer
Harry Yates 2025®