Function Overloads 8 exercises
Problem

Function Overloads vs. Union Types

Consider this function called runGenerator.

It can take either an object where run is a function that returns something, or it can take a function itself:


function runGenerator(generator: unknown) {
if (typeof generator === "function") {
return generator();
}
return gen

Loading exercise

Transcript

0:00 In this exercise, we have a function called runGenerator, and runGenerator is going to take either one of two things. It's either going to take an object where run is a function that returns something, or it's just going to take a function here. 0:15 This is a polymorphic function, and your mind should be spinning already by thinking, "OK. This is in the function overload section. I want to see what's possible here with function overloads. Let's give this a go."

0:27 This exercise, there are two solutions here. One uses function overloads and one doesn't. This is going to be a compare and contrast exercise. See if you can find both solutions, and see if you can guess what I'm going to say in the solution video.