Last Updated:

Typealias in Kotlin

Typealias in Kotlin

Let's imagine that we have a list of users List<User>. Using it in the code is inconvenient, autocompletion works poorly. So we'd like to come up with a simpler name for it.

The traditional way to do this is to wrap the list in a class:

But Kotlin has a simpler and no overhead option:

Typealias does not introduce a new data type or create classes, it is just a middle name for a particular data type. It can be used not only to shorten the record, but also to separate names if multiple packages contain classes with the same name.

Other useful examples of using typealias:

  • A more visual way to declare lambdas:
  • Marking nullable-types:
  • Quick access to nested classes: