TypeScript · Senior · Question 35 of 75
Why should you avoid the Function type for callbacks?
- ✓Correct answer: It accepts any function, and calls aren't checked
- 2It forbids passing arrow functions and methods
- 3It's deprecated and causes a compile error in strict mode
Explanation
Function describes any function, and calling it is unsafe. Prefer a precise signature (value: string) => void, and for "any function" use (...args: never[]) => unknown or (...args: any[]) => any. Likewise, Object and {} are better replaced with object or unknown.