Is it possible to pass strictly typed functions as parameters in TypeScript?
Hello everyone! I recently started learning TypeScript and encountered a problem when working with higher-order functions. I have a function that takes another function as an argument, and I want TypeScript to check that the passed function has the correct parameter and return types.
Here's an example of what I'm trying to do:
```typescript
function applyOperation(x: number, y: number, operation: (a: number, b: number) => number): number {
return operation(x, y);
}
// This works
const sum = applyOperation(5, 3, (a, b) => a + b); // 8
// But what if I want to pass a function that takes strings?
const badResult = applyOperation(5, 3, (a, b) => a.toString() + b.toString()); // No error, but the result is incorrect
```
In the last case, Typ
No answers yet.
Log in to leave an answer
Blogs