Function return type

2 snippets in TypeScript

TSTypeScript

Return Type Annotations

TS · Type Annotations
Syntax
function fn(): ReturnType { ... }
const fn = (): ReturnType => { ... };
Example
function findUser(id: string): User | undefined {
  return userDatabase.get(id);
}

// Arrow function return type
const formatCurrency = (amount: number): string => {
  return `$${amount.toFixed(2)}`;
};

// Async function
async function fetchOrder(id: string): Promise<Order> {
  const resp = await fetch(`/api/orders/${id}`);
  return resp.json();
}
Output
// Async functions always return Promise<T>

Note Explicit return types are especially valuable for public API functions, async functions, and functions with multiple return paths. They also speed up type-checking in large projects since the compiler does not need to analyze the function body.

ReturnType & Parameters

TS · Utility Types
Syntax
type Ret = ReturnType<typeof fn>;
type Params = Parameters<typeof fn>;
Example
function createOrder(userId: string, items: string[], coupon?: string) {
  return {
    orderId: `ord_${Date.now()}`,
    userId,
    items,
    discount: coupon ? 0.1 : 0,
  };
}

type OrderResult = ReturnType<typeof createOrder>;
// { orderId: string; userId: string; items: string[]; discount: number }

type OrderParams = Parameters<typeof createOrder>;
// [userId: string, items: string[], coupon?: string]
Output
// ReturnType extracts what the function returns; Parameters extracts the argument tuple

Note Both require typeof when used with a concrete function (not a type). For class constructors, use ConstructorParameters<typeof ClassName>. ReturnType is invaluable for inferring types from existing functions without duplicating definitions.

Frequently asked questions

How does TypeScript handle function return type?
TypeScript covers this with 2 copy-ready snippets on this page. The "Return Type Annotations" snippet in TypeScript uses `function fn(): ReturnType { ... }`.
Which code does the TypeScript example use?
The "Return Type Annotations" snippet uses `function fn(): ReturnType { ... }`, from the Type Annotations section of the TypeScript cheat sheet.
What other TypeScript snippets are shown for "function return type"?
Besides "Return Type Annotations", this page also shows "ReturnType & Parameters".
Is there anything to watch out for?
Yes. For "Return Type Annotations": Explicit return types are especially valuable for public API functions, async functions, and functions with multiple return paths. They also speed up type-checking in large projects since the compiler does not need to analyze the function body.