TypeScript Functions

TypeScript functions work like JavaScript functions, with types added to parameters and return values. The compiler then checks every call, so wrong arguments are caught as you type.

Parameter and Return Types

Annotate each parameter. The return type is usually inferred, but writing it explicitly documents intent and catches mistakes inside the function.

function addTax(amount: number, rate: number): number {
  return amount * (1 + rate);
}
const subtotal = (items: number[]): number => items.reduce((a, b) => a + b, 0);

console.log(addTax(100, 0.17), subtotal([5, 10]));
// addTax("100", 0.17); // Error: Argument of type 'string' is not assignable to 'number'.
117 15

Optional, Default and Rest Parameters

An optional parameter (?) can be omitted. A default value also makes it optional and its type is inferred. Rest parameters are typed as arrays.

function makeLabel(text: string, prefix?: string, upper = false): string {
  const label = prefix ? `${prefix}: ${text}` : text;
  return upper ? label.toUpperCase() : label;
}
function sumAll(...nums: number[]): number {
  return nums.reduce((a, b) => a + b, 0);
}

console.log(makeLabel("Sale"));
console.log(makeLabel("Sale", "Notice", true));
console.log(sumAll(1, 2, 3, 4));
Sale
NOTICE: SALE
10

void and Function Types

void means a function returns nothing. You can describe a function's shape as a type and use it for callbacks.

type MathOp = (a: number, b: number) => number;

const multiply: MathOp = (a, b) => a * b; // a and b inferred as number
function apply(op: MathOp, x: number, y: number): void {
  console.log("Result:", op(x, y));
}
apply(multiply, 6, 7);
apply((a, b) => a - b, 10, 4);
Result: 42
Result: 6

Object Parameters

Destructured parameters are typed by annotating the whole object pattern.

type CardProps = { title: string; price: number; currency?: string };

function card({ title, price, currency = "USD" }: CardProps): string {
  return `${title} - ${price.toFixed(2)} ${currency}`;
}
console.log(card({ title: "Coffee Beans", price: 22 }));
Coffee Beans - 22.00 USD

Function Overloads

Overloads list several call signatures for one function, followed by a single implementation. Use them when the return type depends on the argument type. Often a union or generic is simpler.

function format(value: number): string;
function format(value: Date): string;
function format(value: number | Date): string {
  return typeof value === "number" ? value.toFixed(1) : value.toISOString().slice(0, 10);
}
console.log(format(3.14159));
console.log(format(new Date("2026-03-20")));
3.1
2026-03-20

Frequently asked questions

Do I need to write return types in TypeScript?

Not always; TypeScript infers them. Writing them on exported or complex functions is good practice because it documents the contract and catches errors inside the function.

What is the difference between an optional parameter and a default parameter?

Both can be omitted. An optional parameter is undefined when missing; a default parameter gets the default value instead.

How do I type a callback function in TypeScript?

Use a function type, such as (value: string) => void, either inline or as a named type alias.