TypeScript Only Features 9 exercises
solution

Create an Enum in TypeScript

Creating an enum is similar to creating an object or interface. The enum members starting with a value of 0, so you don't need to add numbers for the levels:


enum LogLevel {
Debug,
Info,
Warn,
Error,
}

Now that the enum has been created, we need to update the log function t

Loading solution

Transcript

00:00 Okay, so what we can do here is we can say enum log level and then just like an interface, we can just open a closure here and inside there we can look at all of the things that we get. So we've got some syntax errors immediately, add enum log level and enum member name must

00:16 be followed by a comma, equals or a closed curly brace. Interesting. We actually don't need to specify 0, 1, 2, 3 and I'll show you why in a minute. Enum log level now, we're actually done in terms of our declaration here.

00:32 And that's because automatically when you declare an enum, debug gets set to or the first member gets set to 0, the next one gets set to 1, the next one gets set to 2, the next one gets set to 3 and so on. And so we're kind of done with this little enum. Lovely. Now we can go into our log function and say global log level.

00:50 Well how do we express like the type of an enum? How do we do that? Because I don't know, that seems quite difficult, right? We would need to change it to a type or something. No, you can just say log level. That's a nice little benefit of enums is that you can use them on the runtime level and on the type level.

01:08 So we can say log level and then level is also log level. And now all of our types are working nicely. This is pretty cool because it means that you can actually just like immediately reference like log level everywhere. You get autocomplete as to all the kind of members there too, which you don't do if you

01:26 were to say just choose one or something like that. Oddly, if you trigger autocomplete here, there's actually no way that you can kind of like find all the available members. And so log level is a nice little thing here. I can't remember what this was actually set to initially. Info. Great. Okay. So enums. Pretty nice.

01:46 You notice too that you can actually set members to this. So you can say debug equals 0, info equals 1, and warn equals 2, error equals 3. So this gives you a bit more specificity and it means that if you were to add something in the middle here, let's say something else, you could say 1.5 for instance. Can you do that? I'm not sure.

02:06 Can you use it as an integer? Yeah. Apparently you can. Cool. I didn't actually know that. But it means that you're not screwing up the rest of your application, for instance, if they require enum to be a specific number. So there you go. Enums. And actually doing it like this where you say if options.level is greater or equal to

02:26 the global log level means that you're doing quite a lot of nice logic there by checking if, for instance, by representing these as numbers, you can do numeric comparisons on them. That's pretty nice. So there you go. A nice sort of introduction to enums.

02:41 And if you do use enums, I would generally tend to specify them as their exact numbers just to make sure that you're not getting yourself in any pickles further down the line.