Check out a free preview of the full Introduction to Elm, v2 course

The "Aliases" Lesson is part of the full, Introduction to Elm, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Richard demonstrates how type aliases can tersely describe compound types.


Transcript from the "Aliases" Lesson

>> Richard Feldman: Here's how we could annotate the model that we used in the previous exercise. We could write out this entire elaborate nested record, and this is perfectly accurate. If we wanted to put this in our program, Elm would accept it, it would say, yes, I agree, that is what your model looks like.

It's a record of a selectedTag field, which is a String, a List of articles, which is a List of also records. Each of those records has a title String, tags, which is a List of Strings, and then body, which is also a String. But as you can imagine if your program gets kind of big, you don't want to write all this out all the time.

That's just a lot of stuff to be repeating, especially if you have multiple different places where a model is being used. You don't have to copy and paste all that around, fortunately, we don't have to. We can do what's called a type alias, which is a way to give a name to a type.

So just like we can have declarations, where we say user name equals, quote, RTFeldman, that's giving a name to a value. Type alias is a way to give a name to a particular type. And it's most often used with more elaborate types, sort of of compound types like this that have other types inside of them.

Like records or parameter types. Once we define our type alias for Article, then the type alias for Model gets much more concise. Now we can say selectedTag is a String, and articles is just List Article. It's a list of articles and then we've defined elsewhere what it means to be an article.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now