Kotlin Type Aliases

Something I recently learned about in Kotlin, that I do not see talked about very often, are type aliases. Type aliases allow for custom names to be specified for existing types. This is convenient for long or complex types.

These are particularly useful for function types that have particularly complex syntax. For example if we have a function that takes a handler function as a parameter.

fun handleEvent(handler: (type: EventType, message: String) -> Unit) {
  // Some logic...
}

We can use a type alias to make a bit more readable by defining a name to the function type.

typealias EventHandler = (EventType, String) -> Unit

fun handleEvent(handler: EventHandler) {
    // Some logic...
}

Note that type aliases are not creating new types. Behind the scenes the Kotlin compiler takes the custom name and replaces it with the actual type. This means you can work with the original type and the alias interchangeably.

typealias CharacterString = String

fun pl(s: CharacterString) = println(s)

fun main() {
  val test1: CharacterString = "Test String"
  val test2: String = "Test String 2"

  pl(test1)
  pl(test2)
}

Obviously, overusing this feature could result in less readable code, but used appropriately can be very useful. For more details see the Type Aliases section of the Kotlin documentation.

This article is also published on dev.to.