Recall & Review
beginner
What is a callback function in TypeScript?
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?
✗ 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?
✗ Incorrect
The correct syntax is (input: string) => void, meaning it takes a string and returns nothing.
Why is typing callback functions important?
✗ 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
✗ 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?
✗ 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.