Transcript
00:00 OK, so what we need to do is define a type for this function, just like the shape of these functions. If we hover over this, we're going to see that type right here. This is the type of our operator function. Every one of these is going to have the same type. And so we're going to make a type operation function, which our AI assistance is helping us with.
00:19 So we've got a record of operation functions. So this we can define as a record of a string, I guess, as the key, and an operation function. Now, at this point, all of our stuff type checks. But we have lost some autocomplete and other safety
00:39 right here. So we can do whatever we want to right there. So that's not good. That's not what we want at all. So what we're going to have to do is we've got a circular dependency here now. Because we're defining operations as a record of some specific types. But then we derive our operator from that.
00:58 So where does this come from? We're defining it right here. I don't know. Let's just try this. And then we can improve it later. So we're going to make a type called an operator. That is plus, minus, multiply, divide.
01:14 And then our keys can only be operators. And that will take care of that. And now we have our type narrowing working enough so that we can get some autocomplete and some other type safety there. So that's awesome. This part, not so awesome.
01:32 But we'll look at how to fix that here in a second. But we have successfully narrowed down our operations to just be a record of the possible operators to operation functions. And now, with that, we no longer have to have all of these types defined. Because TypeScript knows that the left has to be a number.
01:51 The right has to be a number. Because this is an operation function. And so if we were to try to add something like this, then we'd be able to do left and right. Of course, now we have this, which is a really annoying nightmare thing. But yeah, we don't have to worry about the types. We have left.toExponential or whatever.
02:10 Like, we can do whatever we want to on that. Because we know that it's a number, which is kind of nice. And that was the objective here. But we did lose a little bit of the freedom that we got before in some of our previous exercises. So let's take a look at that next. Yeah, good job. We'll move on.