Type Aliases
Name complex types.
Type Aliases is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Type Alias?
A type alias gives an existing type a new name. It introduces no new type — it is purely a readability tool resolved at compile time.
You declare one with the typealias keyword.
typealias Name = String
fun main() {
val n: Name = "Alice"
println(n)
}Naming Function Types
Function types can be verbose. An alias makes signatures far easier to read.
typealias Handler = (String) -> Unit
fun register(h: Handler) {
h("event fired")
}
fun main() {
register { msg -> println(msg) }
}Naming Generic Types
Deeply nested generics like Map<String, List<Int>> read better with an alias.
typealias ScoreBoard = Map<String, List<Int>>
fun main() {
val board: ScoreBoard = mapOf("team1" to listOf(10, 20))
println(board)
}Aliases Are Transparent
Because an alias is the same type, values are fully interchangeable. Name and String can be mixed freely with no conversion.
typealias Name = String
fun shout(s: String) = s.uppercase()
fun main() {
val n: Name = "bob"
println(shout(n))
}Parameterized Aliases
A type alias can have its own type parameters, passing them through to the underlying type.
typealias Pairs<T> = List<Pair<T, T>>
fun main() {
val data: Pairs<Int> = listOf(1 to 2, 3 to 4)
println(data)
}Aliasing Nested Classes
A long nested or inner class path can get a short alias, reducing import noise and clutter.
class Outer {
class Inner {
fun hello() = "inner hello"
}
}
typealias Shortcut = Outer.Inner
fun main() {
println(Shortcut().hello())
}Improving Domain Readability
Aliases communicate intent. typealias UserId = Int documents that an integer represents a user identity, even though the compiler still treats it as Int.
typealias UserId = Int
typealias Email = String
fun notify(id: UserId, to: Email) {
println("Notify user " + id + " at " + to)
}
fun main() {
notify(42, "a@b.com")
}No Type Safety Added
Be aware: because aliases are transparent, the compiler will not stop you from passing a UserId where a different Int alias is expected. They are documentation, not enforcement.
typealias UserId = Int
typealias OrderId = Int
fun process(order: OrderId) = println("order " + order)
fun main() {
val user: UserId = 5
process(user) // compiles fine — both are Int
}Top-Level Only
Type aliases must be declared at the top level of a file. You cannot define them inside functions or classes. Place them near related code or in a dedicated file.
typealias Callback = () -> Unit
fun run(cb: Callback) = cb()
fun main() {
run { println("done") }
}When to Use Aliases
Reach for type aliases when:
- A function type or generic is long and repeated
- You want self-documenting names for primitives
- You need a short handle for a nested type
When you need real type safety, use a value class instead.
Alias vs Value Class
An alias is the same type with a new name — zero safety. A value class is a distinct type that prevents mixing values. Next lesson covers value classes.
Quick Check
Test your understanding of type aliases.
Recap
You learned type aliases:
- Declared with
typealiasat the top level - Just a new name, fully transparent
- Great for function types, generics, and readability
- Add no type safety
Next: inline value classes for zero-overhead wrapping.
Frequently asked questions
Is the “Type Aliases” lesson free?
Yes — the full text of “Type Aliases” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Type Aliases”?
Name complex types. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Type Aliases” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Type Aliases
- Inline Value Classes
- Use Cases
- Limitations