Skip to main content
⚑ OpenDevDocs
TypeScript
programmingtypescriptGuideBeginner

Functions and Signatures in TypeScript

Typing functions in TypeScript: parameter types, return type inference, optional/default parameters, function types, and function overloads.

Verified: 2026-09-30
Tested on:typescript 5.x
Platforms:
all
#typescript#functions#types#overloads

Functions and Signatures in TypeScript

TypeScript allows you to enforce types on function inputs (parameters), outputs (return types), and define reusable function signature types.


1. Parameter and Return Type Annotations

function formatPrice(amount: number, currency: string = "USD"): string {
  return `${amount.toFixed(2)} ${currency}`;
}

// Arrow function with explicit return:
const multiply = (a: number, b: number): number => a * b;

// Void return (function returns nothing):
function logWarning(msg: string): void {
  console.warn(`[WARN]: ${msg}`);
}

2. Function Signature Types

Define reusable callable function types for callbacks and handlers:

type ClickHandler = (event: React.MouseEvent<HTMLButtonElement>, id: string) => void;

type Predicate<T> = (item: T) => boolean;

const isEven: Predicate<number> = (num) => num % 2 === 0;

3. Function Overloads

Function overloads let a single function declare multiple callable signatures:

// 1. Overload signatures:
function getElement(id: string): HTMLElement | null;
function getElement(index: number): HTMLElement | null;

// 2. Implementation signature (compatible with all overloads):
function getElement(identifier: string | number): HTMLElement | null {
  if (typeof identifier === "string") {
    return document.getElementById(identifier);
  }
  return document.querySelectorAll(".item")[identifier] as HTMLElement ?? null;
}

On this page