Current section: TypeScript 59 exercises
Problem

Derive Types

Loading exercise

Transcript

00:00 You may have noticed that if we add something like the exponentiation operator, if that's the right way to say it, that we are not going to be able to use that, even though it technically is going to work. Hmm, that's kind of annoying that we have TypeScript saying, no, you can't do that,

00:19 but the application saying, yes, actually, you can. And the reason is because we have narrowed down our operator so much that we're not able to add additional things. We just have to, every time we make a change to that, we have to add another one, which is kind of annoying.

00:35 It would be nice if one could be derived from the other, and absolutely, it totally can. So here is an example of a couple of things that you're going to need for this one. So here we have our user. It's an object with a name and is cute. We can actually turn that user

00:51 into a type. We basically have two worlds going on here. We have the runtime world, and then we have the type world. The type world never makes it to the browser or the runtime environment, but getting that runtime world information into the type world is done

01:08 using this type of keyword. And so we say type of user, that's going to get us the type of that user. Huh, fancy that. And so now we've got this type user, which is name is a string and is cute

01:22 is a Boolean. So that is the derived type of that user. And then you can use key of on a type to get all of the keys of this particular object. And so now our user keys is going to be name or is cute.

01:39 So those are the possible keys of this particular type, which is an object. So your job is to do something similar for our operators. So we don't have to worry about the operator inconsistency

01:54 here, where we can add operations, but our operator doesn't support it. So I don't want you to do what we've done here. You need to do something a little bit better. So we'll see you when you're finished.