Beginner's TypeScript Section 18 exercises
Problem

Typing Functions

Now we'll move on to typing functions.

We have an addListener function here:


const addListener = (onFocusChange: unknown) => {
window.addEventListener("focus", () => {
onFocusChange(true);
});
window.addEventListener("blur", () => {
onFocusChange(false);
});
};

Loading exercise

Transcript

0:00 Now, we come onto typing functions. We've got an addListener function here that we've created. What we're saying is we want to say onFocusChange. OnFocusChange is currently unknown. What we want it to be is a function which takes in a single argument, which is going to be a Boolean.

0:22 Now, what we really need to do is to be able to somehow work out how to type this to express our function for us. That's your challenge. It's to go into the TypeScript docs, work out how you type functions, and what this unknown should actually be typed as.