Essential Types And Notations 20 exercises
Problem

Typing an Event Listener

Here we explore a classic web development example.

We have an addClickEventListener function that takes in a listener function and adds it to the document:


const addClickEventListener = (listener) => {
// red squiggly line under `listener`
document.addEventListener("click", list

Loading exercise

Transcript

00:00 Let's look at a classic example from the web development world where we have addClickEventListener. What we're doing here is we're saying the document we want to add a click event listener and then pass that listener to it. And what we want to do is when we call it, we want to pass in a function here and we don't want to return anything from that function.

00:18 So your job is to, like this function never receives any arguments here, and we obviously can't pass a string or anything like that. But the challenge here is not that, it's finding out what return type do we want the function to return. Because currently it's returning nothing and we never want it to return anything.

00:37 So your job is to try to find the type that describes that situation best and then add that to the return type of the function. Good luck.