The typealias
keyword in Swift 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 the typealias
keyword and walks through typealias examples for various types in Swift:
- Swift Typealias Keyword
- Typealias Type
- Typealias Closure
- Typealias Tuple
- Typealias Protocol
- Typealias Generic
- Generic Typealias With Where Clause
- Typealias With Multiple Types
Swift Typealias Keyword
The benefits of using the Swift typealias
keyword fall into two main categories:
- Readability. Like in the Closure Typealias example, the
typealias
keyword in Swift can be used to shorten a verbose compound type into an easy to understand term. - Clarity. Like in the Variable Typealias and Tuple Typealias examples, a Swift
typealias
can be used to clarify how an existing type will be used.
Check out the Swift typealias examples in this post to see how the typealias
keyword can be used to improve Swift readability and clarity.
Typealias Type
A typealias type in Swift is defined using the typealias
keyword followed by the name of a type:
// Create a new name Meters for the type Double
typealias Meters = Double
A type, like Meters
, defined using the typealias
keyword 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"
}
}
Typealias Closure
A typealias closure in Swift is defined using the typealias
keyword followed by a closure definition:
// Define a typealias for the closure APICompletion
typealias APICompletion =
([String:Any]?, HTTPURLResponse?, Error?) -> Void
Using the Swift typealias
keyword to create a typealias for the closure APICompletion
can help provide an easy to read and intuitive name for the complex compound closure type:
// Before using a typealias
func api(completion:
(([String:Any]?, HTTPURLResponse?, Error?) -> Void)) {
completion(["status": "success"], nil, nil)
}
// Replace the compound type with the closure typealias
// for APICompletion
func api(completion: APICompletion) {
completion(["status": "success"], nil, nil)
}
Typealias Tuple
A typealias tuple in Swift is defined using the typealias
keyword followed by a tuple definition:
typealias Point = (Double, Double)
typealias Edge = (Point, Point)
Typealias Protocol
A typealias protocol in Swift is defined using the typealias
keyword followed by a protocol definition:
typealias AppDataSource = UICollectionViewDataSource
Typealias Generic
A typealias generic in Swift is defined using the typealias
keyword followed by one or more placeholder types:
// Define a typealias for the generic Graph
typealias Graph<A,B> = (Array<A>, Array<B>)
The Swift typealias generic Graph can then be used like other generic types:
typealias Point = (Double, Double)
typealias Edge = (Point, Point)
func traverse(_ graph: Graph<Point,Edge>) {
// Perform traversal
}
Generic Typealias With Where Clause
A typealias generic with where clauses can be defined using the Swift keyword typealias
to further disambiguate 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 (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 With Multiple Types
A typealias with multiple types is defined using the Swift typealias
keyword followed by a series of types joined with &
:
// Define a typealias with multiple types
typealias CompositeProtocolAlias = Type1 & Type2 & Type3
Swift Typealias Examples
That’s it! By using the Swift typealias
keyword with generic, variable, tuple, or closure types you can make your Swift code more expressive and readable.