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.
- All episodes are in the TypeScript from Zero playlist.
- Versions used: Node.js 26.13.0, npm 11.20.0, TypeScript 7.0.2.
Setup
We start in the terminal and open the project in VS Code:
$ touch src/params.ts1. 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.
function greet(name: string, title?: string) {
if (title) {
console.log("Hello, " + title + " " + name);
} else {
console.log("Hello, " + name);
}
}
greet("Mia");
greet("Rahman", "Dr");$ node src/params.ts2. 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.
function power(base: number, exp: number = 2) {
return base ** exp;
}
console.log(power(5));
console.log(power(2, 3));$ node src/params.ts3. 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.
function sum(...numbers: number[]) {
let total = 0;
for (const n of numbers) {
total += n;
}
return total;
}
console.log(sum(1, 2, 3, 4));$ node src/params.ts4. 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.
function logIt(msg: string): void {
console.log(msg);
}
logIt("hi");
function fail(msg: string): never {
throw new Error(msg);
}$ node src/params.ts5. 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.
console.log(power(2, 3, 4));$ node src/params.ts6. 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.
$ npx tsc --noEmit7. Fix
We delete that line, and tsc is quiet again.
$ npx tsc --noEmitRecap
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.