React and TypeScript

React and TypeScript Function Overloads Exercise

Learning Paths:
Check out a free preview of the full React and TypeScript course:
The "Function Overloads Exercise" Lesson is part of the full, React and TypeScript course featured in this preview video. Here's what you'd learn in this lesson:

Students are then instructed to make an add function that works with partial application.

Get Unlimited Access Now

Transcript from the "Function Overloads Exercise" Lesson

[00:00:00]
>> So here I have, what is effectively a JavaScript function, right? And you're gonna take a spin at this, and with this JavaScript function, we want to allow it to be an add function that can use a partial application. Which means, let me scroll down to the bottom here, if I give it if we say add(2 and 3), it should return 5.

[00:00:30] If I say, add(2), it should return a function that waits for the second argument. So I could say const addTwo, And then if I said, addTwo, let's say to (5), that should give me 7. So effectively, there's two different interfaces, you give it two numbers, it gives you back a value, you give it one number, it gives you back a function that takes a second number, sevent.

[00:01:07] Right, or we can also just say like ten is, My math is doing great right now, right? Live coding fine live math is apparently a lot harder. Now you can see TypeScript a little angrier about this cuz it doesn't really understand what's happening. But, we wanna basically support both giving it two arguments or giving it one argument, and then the second one later, right?

[00:01:36] And so kinda looking at the async ad, you can see that we're definitely gonna have two lines, which effectively define both of the different interfaces that we want to support for it, right? And you can make helper types like this is for the asynchronous one where it's a callback where it's gonna say it takes a number and returns nothing, that we're using here.

[00:01:55] You can use helper types, you can do it online but, your mission for the next five minutes, is to take a stab at trying to add function overloads, for this add function right here.