Introduction
Typealias lets you give a new, easy name to an existing type. This makes your code simpler and clearer.
typealias NewName = ExistingTypetypealias Text = String
typealias Age = Int
typealias CompletionHandler = (Bool) -> Void
typealias Score = Int func printScore(_ score: Score) { print("Your score is \(score)") } let myScore: Score = 85 printScore(myScore)
typealias in Swift?typealiastypealias is used to give an existing type a new name, making code easier to read and maintain.class, let, or var, not typealias.Int named Age in Swift?typealias NewName = ExistingType.typealias Text = String let greeting: Text = "Hello" print(greeting)
Text is just another name for String. So greeting is a String with value "Hello".greeting outputs the string value "Hello".typealias Number = Int var value: Number = "10"
Number is an alias for Int, so value must be an integer.operation? typealias Operation = (Int, Int) -> Int
let operation: Operation = { a, b in a + b }Operation is defined as a function type that takes two Int parameters and returns an Int.Operation type: it takes two Ints and returns their sum as Int.Pass the test and this pattern moves unknown