Typealias usage in Swift

A typealias in Swift is literally an alias for an existing type. Simple, isn’t it? They can be useful in making your code a bit more readable. By using them in a smart way they can be really useful in your codebase.

Declaring a typealias

A typealias can be declared in Swift using the typealias keyword followed by the type you want to assign. A very simple example to understand how they can be used is by making an alias for currency, like Dollars. Take the following example of a receipt struct:

struct Receipt {
    let totalCosts: Double
}

We still don’t really now the total costs as the type is just a `Double`. We can improve readability by declaring a typealias:

typealias Dollar = Double

Which gives the receipt struct a lot more context and improves readability:

struct Receipt {
    let totalCosts: Dollar
}

A typealias can be a simple alternative to creating a custom class or subclass.

Is a typealias a new type?

No, basically, it’s only a named alias of an existing type. Our Dollar alias is simply a Double with a different name and can, therefore, be used exactly the same as a Double.

This also works the other way around. If you would create an extension for a typealias, you’re basically creating an extension for its underlying type. The following example shows that the toEuro() method is available to both our Dollar and Double value.

typealias Dollar = Double
typealias Euro = Double

struct Receipt {
    let totalCosts: Dollar
}

extension Dollar {
    func toEuro() -> Euro {
        return self * 0.896
    }
}

let receipt = Receipt(totalCosts: 10)
receipt.totalCosts.toEuro() // 8.96

let doubleNumber: Double = 10
doubleNumber.toEuro() // 8.96

Combining with generics

Generics can also be used in combination with a type alias. We could, for example, use the new Swift Result type to create an exchange result type. This could improve our readability even more:

typealias Dollar = Double
typealias Euro = Double

struct Receipt {
    let totalCosts: Dollar
}

typealias Currency = Double
typealias ExchangeResult<Currency> = Result<Currency, Error>

enum ExchangeError: Error {
    case invalidInput
}

extension Dollar {
    func toEuro() -> ExchangeResult<Euro> {
        guard self > 0 else {
            return ExchangeResult.failure(ExchangeError.invalidInput)
        }
        return Result.success(self * 0.896)
    }
}

let receipt = Receipt(totalCosts: 10)
receipt.totalCosts.toEuro() // .success(8.96)

let doubleNumber: Double = 10
doubleNumber.toEuro() // .success(8.96)

Other valuable usage examples

A typealias is often used in projects as a way to easily reuse completion callbacks:

typealias Completion = () -> Void

To make a delegate conform to multiple types:

typealias TransitionDelegate = UIViewController & UIViewControllerTransitioningDelegate

Or, to make a simple alias for a dictionary which is used in multiple places:

/// A dictionary containing properties to send with the tracking operation
public typealias TrackingProperties = [String: Any]

Conclusion

A typealias can be useful to improve readability throughout your codebase as you can see. See what it can do for your project and try to make your code self documenting.

Thanks!

Collect by WeTransfer Collect by WeTransfer is the best way to organize your ideas. Save content from across your apps and bring it together for your friends, your team, or just for yourself.