Skip to content

Typing Functions in TypeScript

Type function parameters, return values, optional and rest params, and function overloads.

What you'll learn

  • How to annotate function parameters and return types
  • Optional, default, and rest parameters
  • Using function type signatures and callbacks
  • Function overloads for multiple call signatures

Concept

Parameter and Return Types

Every function parameter can have a type annotation, and the return type follows the parameter list: function add(a: number, b: number): number. Return type annotations are optional (TypeScript infers them) but recommended for public APIs because they catch accidental return-type changes during refactoring. A function with no return value has type void.

Optional, Default, and Rest Parameters

Optional parameters use a question mark (name?: string) and must come after required parameters. Default parameters (name: string = "Anonymous") are automatically optional. Rest parameters collect remaining arguments into an array: function sum(...nums: number[]): number. These three features let you define flexible function signatures that accept varying inputs.

Function Overloads

Some functions behave differently for different parameter types — for example, format(date: Date): string and format(timestamp: number): string. Function overloads let you declare multiple signatures for a single implementation. You write the overload signatures first, then a general implementation signature. Callers see the specific overloads with precise types, while the implementation handles all cases internally.

Example

              // Basic function with typed params and return.
function add(a: number, b: number): number {
  return a + b;
}

// Optional, default, and rest parameters.
function greet(name: string, title?: string, ...tags: string[]): string {
  const t = title ?? "Friend";
  return `Hello ${t} ${name} [${tags.join(", ")}]`;
}

// Function type signature for callbacks.
type Comparator<T> = (a: T, b: T) => number;
const numCompare: Comparator<number> = (a, b) => a - b;

// Function overloads — multiple call signatures.
function format(input: Date): string;
function format(input: number): string;
function format(input: Date | number): string {
  return new Date(input).toISOString();
}

console.log(add(1, 2));
console.log(greet("Alice", "Dr.", "vip", "new"));
console.log(format(1700000000000));
            

Annotate parameters and return types. Use ? for optional, = for defaults, ... for rest. Overloads declare multiple precise signatures for one implementation.

Try it

  • Regex Tester

    Type-safe functions often wrap regex logic — test patterns here.

  • Code Diff

    Compare overloaded and non-overloaded function implementations.

Common mistakes

The mistake

Putting optional parameters before required ones

The fix

Required parameters must come first. Optional (?) and default parameters must come after all required parameters in the function signature.

The mistake

Writing overloads without a compatible implementation signature

The fix

The implementation signature must be compatible with all overload signatures. List overloads first, then the implementation with a general (usually union) signature.

Practice Typing Functions in TypeScript

7 exercises

Practice Typing Functions in TypeScript

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors