Functions & Overloads Questions with Answers

18 practice questions on Functions & Overloads (TypeScript) with answers and step-by-step solutions. Try 10 samples below, then practise all 18 free in the AptiNest app.

Sample questions

Question 1Easy

Inside this function, what is the type of b?

function area(a: number, b?: number) {
  return a * (b ?? a);
}
  1. number
  2. number | null
  3. any
  4. number | undefined

Answer: D. number | undefined

Explanation

An optional parameter may be omitted, so its type includes undefined. The ?? falls back to a when b is missing.

Step-by-step solution

  1. Assuming strict mode, strictNullChecks is on, so a parameter that may be omitted includes undefined in its type.
  2. B is declared as number and marked optional, so its type is number plus undefined.
  3. So inside the function the type of b is number | undefined (option D).
  4. The ?? operator falls back to a when b is undefined.

Why the other options are wrong

  • A. B may be omitted, so undefined is part of its type under strict mode.
  • B. Null is not part of the type; an omitted optional parameter is undefined.
  • C. Nothing here is any; the parameter is typed as number.

Tip: An optional parameter's type includes undefined under strictNullChecks; ?? supplies a fallback.

Question 2Easy

What is printed?

function sum(...nums: number[]) {
  return nums.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3));
  1. Compile error: sum takes one argument
  2. 6
  3. 1
  4. [1, 2, 3]

Answer: B. 6

Explanation

A rest parameter collects all remaining arguments into an array, here [1, 2, 3], which reduce adds up.

Step-by-step solution

  1. The rest parameter nums collects all arguments into the array [1, 2, 3].
  2. Reduce starts at 0 and adds each element in turn.
  3. 0 + 1 = 1, then 1 + 2 = 3, then 3 + 3 = 6.
  4. The output is 6 (option B).

Why the other options are wrong

  • A. A rest parameter accepts any number of arguments.
  • C. 1 is only the first element; reduce adds them all.
  • D. The function returns the sum, not the array.

Tip: A rest parameter collects arguments into an array; reduce with an initial value adds them up.

Question 3Easy

What is printed?

function greet(name = "Guest") {
  return "Hello " + name;
}
console.log(greet());
  1. Hello Guest
  2. Hello undefined
  3. Hello
  4. Compile error: missing argument

Answer: A. Hello Guest

Explanation

A default parameter is optional, and its type (string) is inferred from the default value.

Step-by-step solution

  1. The parameter name has the default value 'Guest'.
  2. Its type is inferred as string from that default.
  3. greet() is called with no argument, so name is 'Guest'.
  4. The result is 'Hello ' + 'Guest', which prints Hello Guest (option A).

Why the other options are wrong

  • B. The default replaces the missing argument, so undefined is never used.
  • C. The name is filled in with Guest, so that word is printed.
  • D. A default makes the parameter optional, so there is no compile error.

Tip: A default value makes a parameter optional and sets its inferred type.

Question 4Easy

What is the inferred return type of this function?

function hello() {
  console.log("hi");
}
  1. string
  2. undefined
  3. never
  4. void

Answer: D. void

Explanation

A function with no return statement (or only bare returns) is inferred to return void.

Step-by-step solution

  1. The function body has no return statement.
  2. TypeScript infers a function with no return value as returning void.
  3. So the inferred return type is void (option D).

Why the other options are wrong

  • A. The function returns nothing, so the type is not string.
  • B. The inferred type is void, not undefined.
  • C. Never is for functions that never finish, and this one does finish.

Tip: A function with no return value is typed void; a function that never finishes is typed never.

Question 5Medium

Which signature correctly types the destructured parameter?

  1. function f({ a, b }: { a: number; b: string })
  2. function f({ a, b }: [a: number, b: string])
  3. function f({ a: number; b: string })
  4. function f({ a: number, b: string })

Answer: A. function f({ a, b }: { a: number; b: string })

Explanation

The type annotation goes after the whole pattern. Writing { a: number } inside the pattern renames a to a variable called number.

Step-by-step solution

  1. The destructured parameter pattern is { a, b }.
  2. The type annotation goes after the whole pattern.
  3. The annotation is an object type with a: number and b: string.
  4. So function f({ a, b }: { a: number; b: string }) is correct (option A).

Why the other options are wrong

  • B. A tuple type does not match an object pattern with the names a and b.
  • C. Semicolons inside a pattern are a syntax error, and no type annotation is given.
  • D. Inside the pattern a: number renames a to a variable called number, and no types are given.

Tip: Put the type annotation after the whole destructuring pattern.

Question 6Medium

Why should more specific overload signatures be listed before general ones?

  1. The first matching overload is chosen
  2. Overloads are sorted by the compiler anyway
  3. The last overload is the one used at runtime
  4. General overloads cannot have return types

Answer: A. The first matching overload is chosen

Explanation

Overload resolution goes top to bottom and stops at the first match. A broad signature listed first would hide the more precise ones.

Step-by-step solution

  1. Overload resolution tries the signatures from top to bottom.
  2. It stops at the first signature that matches the call.
  3. A broad signature placed first would match first and hide the precise ones.
  4. So specific overloads go first (option A).

Why the other options are wrong

  • B. The compiler does not reorder overloads.
  • C. The implementation signature is not used to pick an overload.
  • D. General overloads can have return types; the issue is the order.

Tip: List overloads from most specific to most general.

Question 7Medium

What must callers pass to this function?

function point(...args: [string, number]) {}
point("x", 1);
  1. At least one string or number
  2. Any number of strings and numbers
  3. Exactly a string, then a number
  4. An array holding a string and number

Answer: C. Exactly a string, then a number

Explanation

Typing a rest parameter as a tuple fixes the count and order of the arguments.

Step-by-step solution

  1. The rest parameter is typed as the tuple [string, number].
  2. A tuple fixes both the count and the order of the arguments.
  3. So point takes exactly a string followed by a number.
  4. Callers must pass exactly a string, then a number (option C).

Why the other options are wrong

  • A. The tuple needs exactly two values, not at least one.
  • B. The count and order are fixed, so any number of values is not allowed.
  • D. The arguments are passed one by one, not as a single array.

Tip: Type a rest parameter as a tuple to fix the argument count and order.

Question 8Medium

What does TypeScript report for the last line?

function len(x: string): number;
function len(x: any[]): number;
function len(x: any) { return x.length; }
len(5);
  1. Error: no overload matches this call
  2. Error: overloads must be in a class
  3. No error; the any implementation accepts 5
  4. No error; it returns undefined

Answer: A. Error: no overload matches this call

Explanation

Callers only see the overload signatures, not the implementation signature. Neither overload accepts a number.

Step-by-step solution

  1. Callers see only the two overload signatures, for string and for any[].
  2. len(5) passes a number, which fits neither overload.
  3. The implementation signature is hidden from callers.
  4. So TypeScript reports that no overload matches this call (option A).

Why the other options are wrong

  • B. Overloads are fine as plain functions; the error is about the call.
  • C. The implementation signature cannot be called from outside.
  • D. The call does not compile, so there is no return value to check.

Tip: Callers only see the overload signatures; the implementation signature is hidden.

Question 9Hard

Does this code compile under --strict?

const out: number[] = [];
[1, 2].forEach(n => out.push(n));
  1. Only with an explicit void return annotation on the arrow
  2. No; forEach callbacks must use a block body
  3. Yes; a value-returning callback can fill a void slot
  4. No; push returns a number, but forEach wants void

Answer: C. Yes; a value-returning callback can fill a void slot

Explanation

A function type with a void return accepts functions that return something; the result is simply ignored. That lets concise arrows like this work.

Step-by-step solution

  1. ForEach expects a callback whose return type is void.
  2. A function type with a void return accepts functions that return a value.
  3. The arrow returns the result of out.push, a number, which forEach ignores.
  4. So the code compiles under strict; the answer is option C.

Why the other options are wrong

  • A. No annotation is needed on the arrow.
  • B. Concise arrow bodies are allowed for callbacks.
  • D. A void callback type accepts a returned number and ignores it, so there is no error.

Tip: A callback typed to return void can return any value; the value is ignored.

Question 10Hard

What does TypeScript report for the last line?

function f(x: string): string;
function f(x: number): number;
function f(x: any) { return x; }
declare const v: string | number;
f(v);
  1. No error; the first overload is used
  2. Error: v must be narrowed to number
  3. Error: no overload matches the call
  4. No error; it returns string | number

Answer: C. Error: no overload matches the call

Explanation

Each overload is tried separately and a union argument fits neither. Add a third signature f(x: string | number): string | number to support it.

Step-by-step solution

  1. The two overloads accept a string and a number separately.
  2. The argument v has type string | number.
  3. The union fits neither single overload, and TypeScript does not combine them.
  4. So TypeScript reports that no overload matches the call (option C).

Why the other options are wrong

  • A. The first overload does not accept a value that may be a number.
  • B. Narrowing does not fix this; the overloads simply do not match the union.
  • D. The call does not compile, so there is no return type to check.

Tip: Overloads do not accept a union by default; add a signature that takes the union.

Practise all 18 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all TypeScript topics · All topics