Skip to content

FOUNDATIONS · LESSON 4 OF 24

Functions, callbacks and overloads

Describe inputs, defaults, rest parameters and related call signatures.

  • functions
  • callbacks
  • rest
  • overloads

Your runnable checkpoint

Extract into a new practice folder. Each download includes complete source, package.json, tsconfig.json and an offline lesson. Use Node.js 22 or 24. Each checkpoint is independent.

Install with npm install. Run npm run check, then npm run build. Terminal lessons use npm start; browser lessons use npm run serve and http://127.0.0.1:4173.

1. Understand the concepts

Function types describe a callable contract. (value: number) => number accepts a number and returns one. Callback parameters usually inherit types from the function receiving them.

A default parameter makes an argument optional at the call site. A rest parameter gathers remaining arguments into an array. void means a caller should not rely on a useful return value; it is different from never, which describes a path that cannot return normally.

Overloads connect different input and output types. Callers see the overload signatures, while one implementation handles their union. Prefer a simple union parameter when every input has the same return type.

The implementation must narrow its input before using operations specific to strings or numbers. An annotation alone does not select a branch at runtime.

2. Build and run

These are complete files. Preserve the paths shown below, including the src directory. The package and compiler configuration are included at the end.

src/main.ts

src/main.ts
function format(value: number): string;
function format(value: string): string;
function format(value: number | string): string {
  return typeof value === "number" ? value.toFixed(2) : value.trim();
}
function calculate(operation: (value: number) => number, ...values: number[]): number[] {
  return values.map(operation);
}
function greet(name: string, prefix = "Hello"): string {
  return `${prefix}, ${name}`;
}
console.log(format(12));
console.log(calculate(value => value * 2, 2, 3).join(", "));
console.log(greet("Asha"));

package.json

package.json
{
  "name": "edupoly-typescript-functions-and-callbacks",
  "private": true,
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "check": "tsc --noEmit",
    "build": "tsc",
    "start": "node dist/main.js"
  },
  "devDependencies": {
    "typescript": "5.9.3"
  }
}

tsconfig.json

tsconfig.json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "lib": [
      "ES2022",
      "DOM"
    ],
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,
    "noEmitOnError": true,
    "verbatimModuleSyntax": true,
    "rootDir": "src",
    "outDir": "dist",
    "sourceMap": true,
    "types": [],
    "moduleDetection": "force"
  },
  "include": [
    "src/**/*"
  ]
}

3. Trace the code and check the result

  1. Only the overload signatures are callable from outside format.
  2. calculate accepts behavior as an argument and applies it to every value.
  3. prefix is inferred as string from its default.

Expected result

12.00
4, 6
Hello, Asha

4. Practice independently

Write a callback-based filter for numbers.

Show the explanation

Use function select(values: number[], predicate: (value: number) => boolean): number[] { return values.filter(predicate); }. Try select([1, 2, 3], value => value > 1).

When something goes wrong

If tsc is missing, run npm install in the folder containing package.json. If the output looks old, rebuild before running. Read the first compiler diagnostic and check the indicated field or argument. For module errors, preserve the .js extensions in imports. Browser examples must run through the included local server, not directly as TypeScript files.

Read the related official TypeScript documentation ↗