A callback function is a function passed as an argument to another function, which is then called inside the outer function to complete some kind of routine or action.
Click to reveal answer
beginner
How do you define a callback function type in TypeScript?
You define a callback function type by specifying the parameter types and return type of the function. For example:
type Callback = (arg: string) => void;
means a function that takes a string and returns nothing.
Click to reveal answer
intermediate
Why is it useful to type callback functions in TypeScript?
Typing callback functions helps catch errors early by ensuring the function passed matches the expected parameters and return type. It also improves code readability and tooling support.
Click to reveal answer
intermediate
Example: How to type a function that accepts a callback with two number parameters and returns a number?
You can write: <pre>function operate(a: number, b: number, callback: (x: number, y: number) => number): number { return callback(a, b); }</pre> This means the callback takes two numbers and returns a number.
Click to reveal answer
intermediate
What does this callback type mean?
(error: Error | null, result?: string) => void
It means the callback function takes two parameters: an error which can be an Error object or null, and an optional result string. The function returns nothing (void). This pattern is common in Node.js style callbacks.
Click to reveal answer
What does a callback function typically do?
AIs a function that never returns
BRuns only once when the program starts
CIs passed as an argument and called later inside another function
DIs a function that only accepts numbers
✗ Incorrect
A callback function is passed as an argument to another function and called later to complete an action.
How do you specify a callback type that takes a string and returns void in TypeScript?
Avoid => string
B(input: string) => void
C(input: number) => string
D(input: string) => string
✗ Incorrect
The correct syntax is (input: string) => void, meaning it takes a string and returns nothing.
Why is typing callback functions important?
ATo catch errors and improve code clarity
BTo make the code run faster
CTo avoid using functions
DTo prevent any function from being called
✗ Incorrect
Typing callbacks helps catch mistakes early and makes code easier to understand.
What does this callback type mean? (error: Error | null, result?: string) => void
ACallback with an error or null and an optional result string, returns nothing
BCallback that returns an error object
CCallback that takes no parameters
DCallback that returns a string
✗ Incorrect
This is a common Node.js style callback with error-first parameter and optional result.
How would you type a function parameter that is a callback taking two numbers and returning a number?
A() => number
B(x: string, y: string) => number
C(x: number) => void
D(x: number, y: number) => number
✗ Incorrect
The callback takes two numbers and returns a number, so the type is (x: number, y: number) => number.
Explain what a callback function type is and why it is useful in TypeScript.
Think about how functions can be passed as arguments and how TypeScript helps with safety.
You got /3 concepts.
Describe how you would type a function that accepts a callback with two number inputs and returns a number.
Focus on the function signature and the callback parameter type.
You got /3 concepts.
Practice
(1/5)
1. What does a callback function type in TypeScript specify?
easy
A. The input parameters and return type of the callback function
B. The variable names used inside the callback function
C. The order in which functions are called
D. The number of times a function is called
Solution
Step 1: Understand callback function types
A callback function type defines what inputs (parameters) the callback expects and what it returns.
Step 2: Identify what is specified
It does not specify variable names, call order, or call count, only input and output types.
Final Answer:
The input parameters and return type of the callback function -> Option A
Quick Check:
Callback type = inputs and output [OK]
Hint: Callback types describe inputs and outputs only [OK]
Common Mistakes:
Confusing callback type with variable names
Thinking callback type controls call order
Assuming callback type limits call count
2. Which of the following is the correct TypeScript syntax to declare a callback type that takes a number and returns void?
easy
A. type Callback = (input: number) => void;
B. type Callback = (number) => void;
C. type Callback = (input: number) => number;
D. type Callback = void (input: number);
Solution
Step 1: Check correct syntax for callback type
In TypeScript, a function type is declared as (parameters) => returnType. Parameters must have names and types.
Step 2: Match the correct option
type Callback = (input: number) => void; correctly declares a function taking a number named 'input' and returning void.
Final Answer:
type Callback = (input: number) => void; -> Option A
3. What will be the output of this TypeScript code?
type Callback = (x: number) => number;
function applyCallback(n: number, cb: Callback) {
return cb(n) + 1;
}
const result = applyCallback(5, x => x * 2);
console.log(result);
medium
A. 10
B. 11
C. 6
D. Error
Solution
Step 1: Understand the callback and function call
The callback multiplies input by 2. applyCallback calls cb(5), which is 5*2=10, then adds 1.
Step 2: Calculate the final result
10 + 1 = 11, so console.log prints 11.
Final Answer:
11 -> Option B
Quick Check:
5*2+1 = 11 [OK]
Hint: Calculate callback result then add 1 [OK]
Common Mistakes:
Forgetting to add 1 after callback
Confusing callback return with input
Assuming callback returns void
4. Identify the error in this TypeScript code snippet:
type Callback = (msg: string) => void;
function greet(cb: Callback) {
cb(123);
}
medium
A. Function greet should not accept a callback
B. Callback should return a number, not void
C. Callback expects a string, but a number is passed
D. No error, code is correct
Solution
Step 1: Check callback type and usage
Callback expects a string parameter, but greet calls cb with 123 (a number).
Step 2: Identify type mismatch error
Passing a number where a string is expected causes a type error.
Final Answer:
Callback expects a string, but a number is passed -> Option C
Quick Check:
Type mismatch in callback argument [OK]
Hint: Match callback parameter types exactly [OK]
Common Mistakes:
Passing wrong argument type to callback
Ignoring callback return type
Assuming no error on type mismatch
5. You want to create a function processItems that takes an array of numbers and a callback. The callback should receive each number and return a string. Which is the correct callback type and function signature to ensure type safety?
hard
A. type Callback = (item: string) => number;
function processItems(items: string[], cb: Callback): number[];
B. type Callback = () => string;
function processItems(items: number[], cb: Callback): string[];
C. type Callback = (item: number) => void;
function processItems(items: number[], cb: Callback): void;
D. type Callback = (item: number) => string;
function processItems(items: number[], cb: Callback): string[];
Solution
Step 1: Define callback input and output types
The callback should take a number and return a string, so type Callback = (item: number) => string.
Step 2: Define function signature matching callback and array
processItems takes number[] and callback, returns string[] because it maps numbers to strings.
Final Answer:
type Callback = (item: number) => string;
function processItems(items: number[], cb: Callback): string[]; -> Option D
Quick Check:
Callback input number, output string [OK]
Hint: Match callback input/output with function parameters/return [OK]
Common Mistakes:
Mismatching callback parameter types
Wrong return type in callback or function
Ignoring array element types
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.