TypeScript · Middle · Question 50 of 75

What is contextual typing?

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.

Further reading

Problem with a question

TypeScript · Middle · question 50

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz