Higher-order function types help us describe functions that take other functions as inputs or return functions as outputs. This makes our code clearer and safer.
Higher-order function types in Typescript
Introduction
Syntax
Typescript
type FuncType = (arg: number) => string; type HigherOrderFunc = (fn: FuncType) => void; // Or a function returning another function: type ReturnsFunc = () => (x: number) => string;
Use parentheses to clearly show function input and output types.
Higher-order functions can accept or return functions with any signature.
Examples
greet takes a function callback that accepts a string and returns nothing.Typescript
type Callback = (msg: string) => void; function greet(callback: Callback) { callback('Hello!'); }
Typescript
type Adder = (x: number) => number; function createAdder(a: number): Adder { return (b: number) => a + b; }
Typescript
type Transformer = (fn: (x: number) => number) => (y: number) => number; const doubleThenAddOne: Transformer = (fn) => (y) => fn(y) + 1;
Sample Program
This program defines a higher-order function shout that takes a function to modify a string. We pass a function that changes text to uppercase.
Typescript
type StringModifier = (input: string) => string; function shout(modifier: StringModifier): void { const message = 'hello'; console.log(modifier(message)); } const toUpperCase: StringModifier = (text) => text.toUpperCase(); shout(toUpperCase);
Important Notes
Always specify input and output types for functions to get full type safety.
Higher-order function types can be tricky at first, but they make your code flexible and clear.
Summary
Higher-order function types describe functions that take or return other functions.
They help catch errors early by checking function shapes.
Use them to write flexible and reusable code.
Practice
1. What is a higher-order function type in TypeScript?
easy
Solution
Step 1: Understand function types
Function types describe the input and output of functions in TypeScript.Step 2: Define higher-order function types
Higher-order function types specifically involve functions that take other functions as parameters or return functions.Final Answer:
A function type that takes or returns another function -> Option AQuick Check:
Higher-order function = function takes/returns function [OK]
Hint: Higher-order means function uses another function [OK]
Common Mistakes:
- Thinking higher-order means only returning numbers
- Confusing higher-order with no parameters
- Assuming it only works with strings
2. Which of the following is the correct TypeScript syntax for a higher-order function type that takes a function returning number and returns void?
easy
Solution
Step 1: Analyze the parameter type
The parameter is a function with no arguments returning a number, written as () => number.Step 2: Analyze the return type
The higher-order function returns void, so the full type is (fn: () => number) => void.Final Answer:
(fn: () => number) => void -> Option DQuick Check:
Parameter is function, return is void [OK]
Hint: Use parentheses to wrap function parameter types [OK]
Common Mistakes:
- Missing parentheses around function parameter
- Writing arrow syntax incorrectly
- Using wrong parameter types
3. What is the output of this TypeScript code?
type Fn = (x: number) => number;
function applyTwice(f: Fn, value: number): number {
return f(f(value));
}
const double: Fn = x => x * 2;
console.log(applyTwice(double, 3));
medium
Solution
Step 1: Understand applyTwice function
It applies function f twice: f(f(value)).Step 2: Calculate output with double function
double(3) = 6, then double(6) = 12, so result is 12.Final Answer:
12 -> Option AQuick Check:
applyTwice(double, 3) = 12 [OK]
Hint: Apply function twice step-by-step [OK]
Common Mistakes:
- Calculating only one application of function
- Confusing multiplication with addition
- Expecting a syntax or runtime error
4. Identify the error in this TypeScript higher-order function type declaration:
type Mapper = (fn: (x: number) => number) => number[];
const mapToArray: Mapper = fn => {
return fn.map(x => x * 2);
};
medium
Solution
Step 1: Check type of fn parameter
fn is a function (x: number) => number, not an array.Step 2: Analyze usage of fn.map
Calling .map on fn is invalid because fn is not an array but a function.Final Answer:
fn is a function, but .map is called on it incorrectly -> Option BQuick Check:
Function cannot use .map method [OK]
Hint: Functions don't have .map method [OK]
Common Mistakes:
- Assuming function can use array methods
- Misreading return type
- Ignoring parameter types
5. Given this TypeScript type for a higher-order function:
Which implementation correctly matches this type?
type Compose = <T, U, V>(f: (b: U) => V, g: (a: T) => U) => (a: T) => V;
Which implementation correctly matches this type?
hard
Solution
Step 1: Understand Compose type
Compose takes two functions f and g, returns a function that applies g then f.Step 2: Check each implementation
const compose: Compose = (f, g) => a => f(g(a)); applies f(g(a)) which matches the type. Others do not follow the composition pattern.Final Answer:
const compose: Compose = (f, g) => a => f(g(a)); -> Option CQuick Check:
Compose = f(g(a)) [OK]
Hint: Compose means f(g(a)) order [OK]
Common Mistakes:
- Reversing function order
- Adding parameters incorrectly
- Using arithmetic instead of function calls
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
