The Weird Parts of TypeScript 13 exercises
Problem

Function Parameter Comparisons in TypeScript

Here we have a listenToEvent function that takes a callback that can handle a varying number of parameters based on how it's called. Currently the CallbackType is set to unknown:


type Event = "click" | "hover" | "scroll";
type CallbackType = unknown;
const listenToEvent = (ca

Loading exercise

Transcript

00:00 Continuing our journey on the weird parts of TypeScript, let's talk about function parameters. Now, what we wanna do here is we have a listenToEvent function, which we can imagine takes in a callback. And that callback can have a few different numbers of parameters based on how it's called. So let's say we want to listen to the event

00:19 and we wanna pass it just a function that accepts no parameters. So don't need to worry about those parameters. Then we pass a function that expects just one parameter, which is event, which is type of event up here. Then we want another one, which can either be event X or Y, then event X, Y, and screen ID.

00:37 So X, Y, and screen ID are all numbers here. Your job is to work out what kind of type we need to give this callback type in order to make this work. And you'll be, I think, surprised at the result. Good luck.