0
0
Typescriptprogramming~5 mins

Callback function types in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
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
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
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
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
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
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.