Loosely related to the idea of a tuple is the idea of having a couple of specific identifiers global
constants you might be working within your app which you want to represent as numbers.
But to which you want to assign a human readable label.
And for Dad you have to enum type.
Again that does exist in some other programming languages.
Javascript doesn't know it though it looks like this is how you create a enum.
You used to enum keyword which only exists in types called world not in JavaScript.
They're off to you you have curly braces and then your identifiers and this type which is added by typescript
gives you an enumerated list.
So these labels are in the end just translated to numbers starting at zero where you have human readable
labels you can work with in your code to show you an example.
Let's go back here to our person with its role.
I'll copy that code and then comment it out because I'm going to change it a bit and then here I'll
get rid of that explicit type assignment and instead go back to inference because I'll now change role.
And now let's say we want to have a admin an author and maybe a read only user the admin should have
an idea of zero read only user has one author has to now of course we can store that exactly like this
with these numbers.
One downside is that we can also add a number for which we might not have a role and if we then later
in our code try to extract a role and use it if check.
This might lead to errors and in addition we as a developer have a hard time to understand which role
is user has lost to the author or was it do you read on the user you might forget this after making
longer pauses when working in bigger teams and so on.
So as a developer we might want human readable identifiers something like admin and read only user something
like that.
Now of course we could use that we could work with such string values.
The problem is if we then later need that and they if check we check if person dot role is equal to
was it read only user or was it one word.
Was it with underscores.
You see then we have to remember how we wrote these strings which exact text we have in there because
this string with the dashes between the words is of course not the same as this string.
So here we would never make it in their is read only if we tried to compile and print that because well
the checks simply doesn't yield true.
We don't have dad as a role on person hence we don't get the output here in a console.
So string identifiers all have downsides now for such scenarios.
It's quite common in javascript to define global constants for example admin which can hold certain
values like numbers or strings both as possible numbers of course can save us some extra code and bits
and memory and then we might have admin read only an author like this and down there we then just have
to use that role.
Now the advantage of that is that we can use this everywhere in our code and therefore here for example
if I run this code if I compile it and then we let this reload we see as admin here.
So this works and this is a common pattern in JavaScript.
Downside is that now again role is inferred to be just a number.
So we could store any number in there even a number which we don't support.
And in addition we have to define all these constants.
We have to manage them.
This is where a enum can save us work.
This is totally fine but at enum makes it easier.
We create a enum with the enum keyword we can name ID roll conventioneers to start with the uppercase
character because the enum also is a custom type it's your first custom type.
We're going to write many more in this course.
This is the first one and then after the name of the type you want to assign to this enum you add curly
brace just like that no colon no equal sign just curly braces.
After the name then a semicolon.
And now in here you can assign your values like admin read only and author and behind the scenes this
year receives the number is 0 this year.
Number one this year.
Number two.
And then here you can access roll dot admin like on an object you axis your identifier and you can use
that anywhere else in your code as well.
Here we can check if the roll is author and then print author.
And of course we shouldn't see that if I run that now because we assigned a role of admin here but the
key takeaway is that we did all of that with the help of the enum which assigns labels to numbers.
We have a look at that in JavaScript so in the compiled code we see does this how it's rebuilt in the
end it's rebuilt with a if a year so a function which executes itself and roll here simply is managed
as an object in the end which has admin property a read only property and all through property where
we then store our no value is here you could say so it's a bit more complex than that but that's what
types in the end does here to replicate this enum construct in JavaScript code when it compiles the
code here as a developer we simply have these conveniently created numbers if you hover over them you'll
see the number which was stored in there and you can use these numbers here in a human readable way
now for in you're also not restricted to the default behavior let's say for some reason you don't want
to start with zero as a starting number then you can add a equals sign here after your identifier and
enter any other number and now admin is assigned to the number 5 and the other identifiers after the
identifier where you assign a value pick up on that and simply increment this starting value.
So now we have 5 6 7 instead of 0 1 2 you of course also can assign your own values to all these identifiers
if you need your own numbers you're also not restricted to numbers you can also go with text here or
even mix it like I'm doing it here anything goes there but often the default of incrementing numbers
that start at zero is what you want but if you need a different behavior if you want to use a string
in here and then a number here and here a number as well or maybe another string then you can absolutely
do that.
The advantages you defined it up there and then thereafter you always just refer to your role.
Type 2 this custom type you created to this custom enum you created and you use that in your if checks
and your type assignments and you're good.
That's the power of the enum and it's a great construct whenever you need identifiers that are human
readable and have some mapped value behind the scenes.