On this page

Functions

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Functions are the heart of any JavaScript application. TypeScript enhances them by letting you explicitly describe:

This transforms functions from “flexible but risky” to “precise and self‑documenting.”


Parameter Types​

In JavaScript, function parameters are untyped. TypeScript lets you specify exactly what each parameter should be.


function greet(name: string, age: number) {

  return `${name} is ${age} years old`;

}

If you call it incorrectly:


greet("Alice", "30"); // ❌ Error

greet(123, 456); // ❌ Error

Optional parameters​

Use ? to mark parameters as optional:


function log(message: string, prefix?: string) {

  console.log(prefix ? `${prefix}: ${message}` : message);

}

Default parameters​

Default values automatically infer types:


function multiply(a: number, b = 1) {

  return a * b;

}

Return Types​

TypeScript can infer return types, but explicitly annotating them improves clarity and prevents accidental changes.


function add(a: number, b: number): number {

  return a + b;

}

If you accidentally return the wrong type:


function add(a: number, b: number): number {

  return "not a number"; // ❌ Error

}

Why explicitly annotate return types?​


Function Overloads​

JavaScript functions often behave differently depending on the arguments passed. TypeScript lets you model this with overloads.

Example: A function that returns different types​


function format(input: string): string;

function format(input: number): string;

function format(input: string | number): string {

  return `Value: ${input}`;

}

Usage:


format("hello"); // ✔ OK

format(123); // ✔ OK

format(true); // ❌ Error

Why overloads matter​

They allow you to express:

This is especially useful for:


void vs never​

These two special return types often confuse newcomers, but they serve very different purposes.


void — A function that returns nothing​


function log(message: string): void {

  console.log(message);

}

void means:

It’s the correct type for:


never — A function that never returns​

A function returns never when it cannot return normally.

1. Functions that always throw​


function fail(message: string): never {

  throw new Error(message);

}

2. Functions with infinite loops​


function loopForever(): never {

  while (true) {}

}

3. Exhaustiveness checking​

never is essential for catching missing cases:


type Shape = { kind: "circle" } | { kind: "square" };



function area(shape: Shape) {

  switch (shape.kind) {

    case "circle":

      return 1;

    case "square":

      return 2;

    default:

      const _exhaustive: never = shape; // ❌ Error if a case is missing

      return _exhaustive;

  }

}

Summary: void vs never​

TypeMeaningExamplevoidFunction returns nothingLogging, event handlersneverFunction never returnsThrows, infinite loops, exhaustiveness checks


Summary​

In this lesson, you learned how TypeScript enhances functions with:

1. Parameter types​

2. Return types​

3. Function overloads​

4. void vs never​

These tools make your functions predictable, self‑documenting, and far safer than their JavaScript counterparts.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Objects & Type Aliases

Next

Arrays & Tuples