Note: This article was originally published at strilliant.com — please give some love to the original! ❤
Enums (enumerations) allow you to group values together with friendlier names. Imagine you had a list of names.
Here’s how you would structure the
You can grab the values from the
enum like so:
But wait. It returns the integer which represents the index of the value. Like arrays, enums begin indexing their members starting at
How do we get the value
"Indrek" instead of
Notice how the values are presented as a string.
Another great example would be using enums to store the application states.
In case you’re interested in learning more about enums— I found a great answer going in the nitty gritty of
Let’s say we fetched some data from an API. We always expect the data to be fetched — but what if we can’t fetch the data?
Perfect time to return the
never type (special case scenario):
We can call the
error function inside another function (callback):
Notice how we don’t use the
never since the inferred return type is
- null — the absence of any value
- undefined — a variable has been declared but has not yet been assigned a value
Not very useful on their own
undefined are subtypes of all other types. That means you can assign
undefined to something like
Here’s a great post about
defined by Dr. Axel Rauschmayer.
Type assertions usually happen if you know the type of some entity could be more specific than its current type.
Type assertions have no runtime impact, and is used purely by the compiler. TypeScript assumes that you, the programmer, have performed any special checks that you need.
Here’s a quick demonstration:
<> syntax collides with JSX so we use the
as syntax instead.
Here’s a lot more about type assertions.
Cool stuff to consider
Now — build something awesome with Typescript!
Thanks for reading, stay awesome!
Due to high demand I’m extending the list of fun apps to build. 😁medium.freecodecamp.org
What is a rich text editor anyway?medium.com
Separating your frontend and backend has many advantages:medium.freecodecamp.org
Let’s talk about Code — Visual Studio Code.medium.freecodecamp.org
The latest Tweets from Indrek Lasn (@lasnindrek): "I just published "Introducing https://t.co/wIhICx6ayg"…twitter.com