WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Kotlin · Intermediate · question 25 of 100

What is the purpose of the typealias keyword in Kotlin, and how is it used?

📕 Buy this interview preparation book: 100 Kotlin questions & answers — PDF + EPUB for $5

The ‘typealias‘ keyword in Kotlin provides a way to define a new name for an existing type. It allows developers to create an alternative name for a type, typically to make the code more understandable or expressive. This can be especially useful when working with complex types that have long or unwieldy names.

Here’s an example of how typealias can be used in Kotlin:

typealias EmployeeList = List<Employee>

data class Employee(val name: String, val age: Int)

fun main() {
    val employees: EmployeeList = listOf(
        Employee("John", 28),
        Employee("Emily", 24),
        Employee("Sara", 32)
    )
    println(employees)
}

In the example above, ‘typealias‘ is used to create a new name ‘EmployeeList‘ for the existing ‘List<Employee>‘ type. This makes it easier to read and understand where the ‘EmployeeList‘ type is used in the code.

When ‘EmployeeList‘ is used later in the code to declare a variable, it is equivalent to using ‘List<Employee>‘.

By default, typealias creates a new name for an existing type only. However, it can also be used to create a specialized type by applying type arguments to an existing generic type, like so:

typealias StringSet = Set<String>

val set: StringSet = setOf("apple", "banana", "cherry")

In summary, typealias is a feature in Kotlin that allows developers to define a new name for an existing type or create a specialized type, making the code more readable and expressive.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Kotlin interview — then scores it.
📞 Practice Kotlin — free 15 min
📕 Buy this interview preparation book: 100 Kotlin questions & answers — PDF + EPUB for $5

All 100 Kotlin questions · All topics