M Logo
๐Ÿ“ TypeScript from Zero ยท Part 14 of 50

TypeScript Function Types: Optional, Default and Rest Parameters, void and never

November 6, 2026

Welcome to episode 14 of TypeScript from Zero, a series for complete beginners (no JavaScript needed). Welcome back to TypeScript from zero! Today we take a closer look at function parameters, and at what a function returns. We stay in the shop project, with a new file called params.ts. Every code block below is the code from the video, ready to copy; run each file to see the output.

Setup

We start in the terminal and open the project in VS Code:

bash
$ touch src/params.ts

1. Optional

A question mark after the parameter name makes it optional. Here the title may be missing, so inside the function we check if it exists. We can call greet with one word, or with two.

ts
function greet(name: string, title?: string) {
  if (title) {
    console.log("Hello, " + title + " " + name);
  } else {
    console.log("Hello, " + name);
  }
}
greet("Mia");
greet("Rahman", "Dr");
bash
$ node src/params.ts

2. Default

A parameter can also have a default value. If the caller leaves out the exponent, it is simply two. So power of five gives twenty five, and power of two and three gives eight.

ts
function power(base: number, exp: number = 2) {
  return base ** exp;
}
console.log(power(5));
console.log(power(2, 3));
bash
$ node src/params.ts

3. Rest

Three dots make a rest parameter. It collects all the remaining arguments into one array. So sum can take as many numbers as we like, and we add them in a loop.

ts
function sum(...numbers: number[]) {
  let total = 0;
  for (const n of numbers) {
    total += n;
  }
  return total;
}
console.log(sum(1, 2, 3, 4));
bash
$ node src/params.ts

4. void, never

A function that returns nothing has the return type void. And a function that never returns at all, because it always throws an error, has the type never. We will use never again in a later episode.

ts
function logIt(msg: string): void {
  console.log(msg);
}
logIt("hi");
function fail(msg: string): never {
  throw new Error(msg);
}
bash
$ node src/params.ts

5. Too many

Now a mistake: we call power with three numbers, but it only accepts one or two. Node ignores the extra number, and prints eight, without a word.

ts
console.log(power(2, 3, 4));
bash
$ node src/params.ts

6. tsc

tsc counts: expected one to two arguments, but got three. The code is TS2554 again, with a range this time, because one parameter is optional.

bash
$ npx tsc --noEmit

7. Fix

We delete that line, and tsc is quiet again.

bash
$ npx tsc --noEmit

Recap

Today: optional parameters with a question mark, default values, rest parameters, void and never, and the argument count error. Next episode: object types, with type and interface.

ยฉ 2026 All Rights Reserved By Mahmudul Alam