TypeScript · Middle · Question 50 of 75
What is contextual typing?
- ✓Correct answer: Inferring a function's parameter types from where it's used
- 2Typing that depends on the environment (browser or Node.js)
- 3Specifying a type via a JSDoc comment
Explanation
In button.addEventListener('click', e => ...), the type of e (MouseEvent) is inferred from the addEventListener signature. map callbacks, JSX handlers and useState setters work the same way. If you move the function into a separate variable, the context is lost - you'll have to annotate it.