Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A type alias in Swift provides an alternative name for an existing type. It does not define a new, distinct type in the compiler’s type system; rather, it acts as a named reference to an underlying type. Because the alias and the original type are strictly interchangeable, the compiler evaluates them as identical during type checking.

Syntax

A type alias is declared using the typealias keyword, followed by the new identifier, the assignment operator (=), and the existing type.
typealias AliasName = ExistingType

Technical Characteristics

Type Equivalence Variables declared with a type alias can be directly assigned to variables of the underlying type without casting.
typealias Integer = Int

let aliasedValue: Integer = 42
let standardValue: Int = aliasedValue // Compiles without type conversion
Closure Signatures Type aliases can encapsulate complex closure signatures, including parameters and return types.
import Foundation

typealias CompletionHandler = (Data?, URLResponse?, Error?) -> Void

// The alias can now be used as a type annotation
var onComplete: CompletionHandler?
Generic Type Aliases Type aliases support generic parameters. This allows you to partially or fully apply type arguments to an existing generic type.
// Partially applied generic type
typealias StringDictionary<Value> = Dictionary<String, Value>

let metadata: StringDictionary<Int> = ["count": 10]
Protocol Composition A type alias can represent a composition of multiple protocols using the & operator. The Swift standard library uses this mechanic internally (e.g., Codable).
typealias HashableAndComparable = Hashable & Comparable

struct CustomKey: HashableAndComparable {
    let id: Int
    
    // Swift synthesizes Hashable and Equatable requirements for the 'id' property.
    // Comparable requires an explicit implementation of the '<' operator.
    static func < (lhs: CustomKey, rhs: CustomKey) -> Bool {
        return lhs.id < rhs.id
    }
}
Tuple Aliasing Type aliases can define named tuples, preserving the element labels of the underlying tuple structure.
typealias Coordinates = (x: Double, y: Double, z: Double)

let origin: Coordinates = (x: 0.0, y: 0.0, z: 0.0)

Access Control

Type aliases adhere to standard Swift access control modifiers (public, internal, fileprivate, private). A type alias cannot have a higher access level than the type it aliases. For example, a public type alias cannot reference an internal or private type.
internal struct InternalType {}
public struct PublicType {}

// Compiler Error: Type alias cannot be declared public because its underlying type is internal
public typealias PublicAlias = InternalType 

// Valid: Type alias has a lower or equal access level compared to its underlying type
private typealias PrivateAlias = PublicType 
Master Swift with Deep Grasping Methodology!Learn More