Kotlin

Type Aliases

admin by @admin ADMIN
Jun 13, 2026
Jun 1, 2026
Public
0 0 up · 0 down Sign in to vote
`typealias` gives a shorter / more meaningful name to an existing type. Compile-time only — no new type, no overhead. Great for taming verbose function types or generic parameters.
Kotlin
Raw
typealias UserId   = Long
typealias EventMap = Map<String, List<String>>
typealias Predicate<T> = (T) -> Boolean

// Real-world: cleans up callback signatures
typealias OnUserClick = (userId: UserId, source: String) -> Unit

fun registerHandler(onClick: OnUserClick) {
    onClick(42L, "menu")
}

fun main() {
    val events: EventMap = mapOf(
        "signup" to listOf("u1", "u2"),
        "login"  to listOf("u3"),
    )
    println(events["signup"])

    val isPositive: Predicate<Int> = { it > 0 }
    println(isPositive(5))                     // true

    registerHandler { id, src -> println("clicked: $id from $src") }
}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.