How to properly type a callback function in TypeScript method parameters?
Hello everyone! I started learning TypeScript and encountered a problem with typing callbacks. I have a method that takes a callback function, and I want to strictly specify its type, but I don't know how to do it correctly.
Here's an example. I have a class `DataProcessor` that processes data and calls a callback after completion:
```typescript
class DataProcessor {
process(data: number[], callback: ???) {
const result = data.map(x => x * 2);
callback(result);
}
}
```
The problem is that I don't know what to write instead of `???`. If I write `callback: any`, TypeScript stops checking types and I lose all the benefits of static typing. If I write `callback: Function`, I also don't get autocompletion and argument checking.
I want the callback to be able to
No answers yet.
Log in to leave an answer