liveBook · Manning

Type alias

As explained in Kotlin in Action, 2E

A type alias is a Kotlin declaration that gives an alternative name to an existing type without creating a distinct type.

A type alias gives an existing Kotlin type a clearer or shorter name without creating a distinct type. It is especially useful for long generic and functional type signatures, improving readability while preserving the original type’s behavior. section 11.3.7

typealias NameCombiner = (String, String, String, String) -> String    #1
 
val authorsCombiner: NameCombiner = { a, b, c, d -> "$a et al." }      #2
val bandCombiner: NameCombiner = { a, b, c, d -> "$a, $b & The Gang" } #2
 
fun combineAuthors(combiner: NameCombiner) {                           #3
    println(combiner("Sveta", "Seb", "Dima", "Roman"))
}
 
fun main() {
    combineAuthors(bandCombiner)                                       #4
    // Sveta, Seb & The Gang
    combineAuthors(authorsCombiner)                                    #4
    // Sveta et al.
    combineAuthors { a, b, c, d -> "$d, $c & Co."}                     #5
    // Roman, Dima & Co.
}

NameCombiner names the functional type `(String, String, String, String) -> String`; the alias is then used for variables, function parameters, and inline lambdas. section 11.3.7

Use type aliases when the name adds useful context, but keep their limits in mind:

  • The alias shortens repetition and communicates intent, making a complex type easier to recognize at each use site. section 11.3.7
  • The compiler expands the alias to its underlying type, so aliases add no constraints or additional type safety. section 11.3.7
  • Choose names carefully: readers unfamiliar with the codebase must mentally resolve an alias back to its underlying type. section 11.3.7

Alias versus a distinct type

With `typealias ValidatedInput = String`, `save(rawInput)` accepts an ordinary `String`; the alias documents intended meaning but does not enforce validation. A value class such as `@JvmInline value class ValidatedInput(val s: String)` creates a distinct type, so the same call requires a `ValidatedInput` value instead. section 11.3.7

How Kotlin in Action, 2E covers it

How deep it goes
Explains declaration syntax, usage, tradeoffs, and compile-time expansion, but stops short of adding type safety.
Examples
Uses the NameCombiner functional alias and contrasts a ValidatedInput alias with a value class.
What it assumes
Assumes familiarity with Kotlin types, generics, and function types while placing type aliases within generic programming.
What you can do after
Apply it by naming repeated complex types while recognizing when a distinct value class is required for stronger type separation.
How it connects
Connects type aliases with generics, functional types, inline classes, and compile-time type expansion.

Also explained in

Compare how 6 books explain type alias →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.