typealias: What is it and how to use it

What are types?

The Swift programming language knows two kind of types: named types and compound types. Named types range from commonly used types defined in the Swift standard library, such as Int, String or Array. In addition to that, user-defined types are part of named types.

Compound types is a nameless type, defined in the Swift language itself. There are two compound types: function types and tuple types. Named types can aswell be part of a compound type.

What does “type-safe” mean?

When declaring a variable in Swift, you can

  1. either assign a type to your variable or
  2. you don’t assign a type at all.

As we assigned the initial value to the variable name , Swift will assign it the type String. This way Swift makes sure that our data is concise and not ambigious. But this also means that when Swift assigns a type, you can change the variable down the road, however, you can't change its type.

Now that we know a little about types and type-safe in Swift, let’s move on to typealias.

typealias

With extending complexity of a project, you might see yourself in situations where you need to balance between clarity and complexity. When needing new types, that are tailored to your needs, you can use typealias.

Type alias introduces an alias to an already existing (e.g. built-in) type. Imagine declaring a struct for a Mage character you want to use in your iOS game project.

You declare your Mage struct as inheriting from the NPC protocol, and declare all the protocol stubs. As you see, we have four user-defined types: Class , HealthPoints, AttackPoints, DefensePoints. You might argue, that a simple Double or Int would be sufficient here - and in this example, you would probably be right - but it makes the code generally cleaner.

But now to the NPC protocol.

Type alias allows us to “define” custom types that are based on already existing types. We don’t really define the types, but name a new type that aliases another type. This way we can use the methods provided by the referenced type, without re-writing code or using boilerplate.

Making more advanced stuff

One popular use case of type aliases is to shorten completion handlers within the function declaration. In general, you put the whole completion handler inside the function declaration, maybe breaking it down into multiple lines. However this isn’t exactly clean code, and therefore lacks code clarity.

A better way is to declare a type alias beforehand and use this inside the function declaration.

Summary

The use of type aliases can make your code more clear and precise. You can use it to name your custom types without actually declaring a new type, when you just want to use a built-in type but name more precisely. This also gives you the opportunity to make use of all methods of the type your referencing to.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store