Codectionary / Developer documentation / TypeScript

ReturnType<T> and Parameters<T>

ReturnType<T> extracts the return type of a function type, and Parameters<T> extracts its parameter types as a tuple. Both are especially useful for deriving types from functions you do not directly control, like third-party library functions, without manually duplicating their signatures.

Syntax

ReturnType<typeof fn>
Parameters<typeof fn>

Examples

ReturnType<T>

Extracting a function's return type without redefining it manually.

function createUser(name: string, age: number) {
  return { id: Date.now(), name, age, createdAt: new Date() };
}

type User = ReturnType<typeof createUser>;
// User is inferred as: { id: number; name: string; age: number; createdAt: Date }
// No need to manually write out this shape - it stays in sync with the function automatically

Parameters<T>

Extracting a function's parameter types as a tuple.

function greet(name: string, greeting: string = "Hello") {
  return `${greeting}, ${name}!`;
}

type GreetParams = Parameters<typeof greet>;
// GreetParams is: [name: string, greeting?: string]

function logCall(...args: Parameters<typeof greet>) {
  console.log("Calling greet with:", args);
}

Best practices

  • Use ReturnType<typeof fn> to derive a type from a function's actual implementation, rather than manually duplicating and maintaining a matching interface
  • Always use typeof when passing a function to these utilities (ReturnType<typeof myFunc>), since they operate on types, not runtime values
  • Use Parameters<T> when writing a wrapper or decorator function that needs to accept the exact same arguments as another function
  • These are especially valuable for third-party library functions whose exact return shape you do not want to manually re-type and keep in sync

At a glance

Purpose
Static types for JavaScript
File extension
.ts ยท .tsx
Runs in
Compiled to JavaScript
Usually used with
JavaScript and its ecosystem

Specifications & further reading

Related TypeScript documentation