Callback function types help us tell TypeScript what kind of function we expect to use later. This keeps our code safe and clear.
Callback function types in Typescript
function example(callback: (param: Type) => ReturnType): void { // function body }
The part (param: Type) => ReturnType describes the callback's input and output.
You can have multiple parameters in the callback by separating them with commas.
function greet(callback: (name: string) => void) { callback('Alice'); }
function calculate( a: number, b: number, callback: (x: number, y: number) => number ): number { return callback(a, b); }
type Callback = (error: Error | null, result?: string) => void; function fetchData(callback: Callback) { // simulate success callback(null, 'Data loaded'); }
This program processes each number in an array and calls the callback to print it.
function processNumbers( numbers: number[], callback: (num: number) => void ): void { for (const num of numbers) { callback(num); } } processNumbers([1, 2, 3], (n) => { console.log(`Number is: ${n}`); });
Always specify the types of parameters and return values in your callback to avoid mistakes.
Callbacks can be anonymous functions or named functions.
Using type aliases for callbacks can make your code cleaner and easier to read.
Callback function types describe what inputs and outputs a callback function should have.
They help TypeScript check your code and prevent errors.
You use them when passing functions as arguments to other functions.
Practice
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 AQuick Check:
Callback type = inputs and output [OK]
- Confusing callback type with variable names
- Thinking callback type controls call order
- Assuming callback type limits call count
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 AQuick Check:
Correct syntax = type Callback = (input: number) => void; [OK]
- Omitting parameter name
- Wrong return type
- Incorrect arrow function syntax
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);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 BQuick Check:
5*2+1 = 11 [OK]
- Forgetting to add 1 after callback
- Confusing callback return with input
- Assuming callback returns void
type Callback = (msg: string) => void;
function greet(cb: Callback) {
cb(123);
}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 CQuick Check:
Type mismatch in callback argument [OK]
- Passing wrong argument type to callback
- Ignoring callback return type
- Assuming no error on type mismatch
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?
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 DQuick Check:
Callback input number, output string [OK]
- Mismatching callback parameter types
- Wrong return type in callback or function
- Ignoring array element types
Ready to prove it?
Pass the test and this pattern moves unknown
