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





