Sample questions
Question 1Easy
Inside this function, what is the type of b?
function area(a: number, b?: number) {
return a * (b ?? a);
}
- number
- number | null
- any
- 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
- Assuming strict mode, strictNullChecks is on, so a parameter that may be omitted includes undefined in its type.
- B is declared as number and marked optional, so its type is number plus undefined.
- So inside the function the type of b is number | undefined (option D).
- 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));
- Compile error: sum takes one argument
- 6
- 1
- [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
- The rest parameter nums collects all arguments into the array [1, 2, 3].
- Reduce starts at 0 and adds each element in turn.
- 0 + 1 = 1, then 1 + 2 = 3, then 3 + 3 = 6.
- 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());
- Hello Guest
- Hello undefined
- Hello
- 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
- The parameter name has the default value 'Guest'.
- Its type is inferred as string from that default.
- greet() is called with no argument, so name is 'Guest'.
- 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");
}
- string
- undefined
- never
- void
Answer: D. void
Explanation
A function with no return statement (or only bare returns) is inferred to return void.
Step-by-step solution
- The function body has no return statement.
- TypeScript infers a function with no return value as returning void.
- 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?
- function f({ a, b }: { a: number; b: string })
- function f({ a, b }: [a: number, b: string])
- function f({ a: number; b: string })
- 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
- The destructured parameter pattern is { a, b }.
- The type annotation goes after the whole pattern.
- The annotation is an object type with a: number and b: string.
- 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?
- The first matching overload is chosen
- Overloads are sorted by the compiler anyway
- The last overload is the one used at runtime
- 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
- Overload resolution tries the signatures from top to bottom.
- It stops at the first signature that matches the call.
- A broad signature placed first would match first and hide the precise ones.
- 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);
- At least one string or number
- Any number of strings and numbers
- Exactly a string, then a number
- 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
- The rest parameter is typed as the tuple [string, number].
- A tuple fixes both the count and the order of the arguments.
- So point takes exactly a string followed by a number.
- 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);
- Error: no overload matches this call
- Error: overloads must be in a class
- No error; the any implementation accepts 5
- 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
- Callers see only the two overload signatures, for string and for any[].
- len(5) passes a number, which fits neither overload.
- The implementation signature is hidden from callers.
- 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));
- Only with an explicit void return annotation on the arrow
- No; forEach callbacks must use a block body
- Yes; a value-returning callback can fill a void slot
- 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
- ForEach expects a callback whose return type is void.
- A function type with a void return accepts functions that return a value.
- The arrow returns the result of out.push, a number, which forEach ignores.
- 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);
- No error; the first overload is used
- Error: v must be narrowed to number
- Error: no overload matches the call
- 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
- The two overloads accept a string and a number separately.
- The argument v has type string | number.
- The union fits neither single overload, and TypeScript does not combine them.
- 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 →