Variable, Tuple, Closure, and Generic Typealias in Swift

A Typealias defines another name for an existing type allowing a developer to add additional clarity to how an existing type is used. This post presents an overview of Typealias and walks through examples for creating a typealias for various types in Swift:

  1. When to use a Typealias
  2. Variable Typealias
  3. Closure Typealias
  4. Tuple Typealias
  5. Generic Typealias
  6. Generic Typealias With Where Clause

When to use a Typealias

The benefits of a Typealias fall into two main categories:

  1. Readability. Like in the Closure Typealias example, a Typealias can be used to shorten a verbose compound type into an easy to understand term.
  2. Clarity. Like in the Variable Typealias and Tuple Typealias examples, a Typealias can be used to clarify how an existing type will be used.

Variable Typealias

A Variable Typealias is defined using the typealias keyword:

// Create a new name Meters for the type Double
typealias Meters = Double

The Typealias defined type Meters can be used like any other type:

// By using Meters instead of Double, the unit of measurement
// for defining a Box is clear
struct Box {
    var width: Meters
    var height: Meters
    var depth: Meters
    
    var sizeLabel: String {
        return "\(width)m-\(height)m-\(depth)m"
    }
}

Closure Typealias

A Closure Typealias is defined using the typealias keyword:

// Define a closure Typealias APICompletion
typealias APICompletion = 
    ([String:Any]?, HTTPURLResponse?, Error?) -> Void

Using the Closure Typealias APICompletion can help provide an easy to read and intuitive name for the complex compound closure type:

// Before using a closure Typealias
func api(completion: 
    (([String:Any]?, HTTPURLResponse?, Error?) -> Void)) {
    completion([“status”: “success”], nil, nil)
}

// Replace the compound type with the APICompletion Typealias
func api(completion: APICompletion) {
    completion([“status”: “success”], nil, nil)
}

Tuple Typealias

A tuple Typealias is defined using the typealias keyword:

typealias Point = (Double, Double)
typealias Edge = (Point, Point)

Generic Typealias

A Generic Typealias is defined using the typealias keyword and one or more placeholder types:

// Define a generic typealias Graph
typealias Graph<A,B> = (Array<A>, Array<B>)

The Generic Typealias Graph can then be used like other generic types specifying named types:

typealias Point = (Double, Double)
typealias Edge = (Point, Point)

func traverse(_ graph: Graph<Point,Edge>) {
    // Perform traversal
}

Generic Typealias With Where Clause

A Generic Typealias can have a where clause to further disambiguate the applicable types:

struct Point<T> {
    var x: T
    var y: T
}

struct Edge<T> {
    var a: T
    var b: T
}

// Create a Typealias Graph for Point and Edge with
// Numeric values only like Int, Double, Float, etc
typealias Graph<T> = 
    (Array<Point<T>>, Array<Edge<T>>) where T: Numeric

func traverse<T>(_ graph: Graph<T>) {
    // Perform traversal
}

Typealias Examples In Swift

That’s it! By using generic, variable, tuple, or closure Typealias you can make your Swift code more expressive and readable.