Kotlin Tip #23: Use type aliases to provide alternative names for existing types — 100 Kotlin Tips in 100 Days

Raphael De Lio
Kotlin with Raphael De Lio
2 min readMar 3, 2024

Twitter | LinkedIn | YouTube | Instagram
Tip #22: Use copy() to Modify Immutable Objects

Type aliases in Kotlin provide a way to introduce alternative names for existing types, whether they are classes, functions, or even complex generic types.

This feature is particularly useful for improving code readability and reducing complexity in your Kotlin codebase. By offering a simpler or more context-specific name for a type, type aliases can make your code more understandable and maintainable.

A type alias is declared using the typealias keyword, followed by the alias name and the type it refers to. The basic syntax looks like this:

typealias AliasName = ExistingType

Complex generic types, especially those with multiple type parameters, can often be verbose and hard to read. Type aliases can simplify these definitions, making them more readable:

Moreover, type aliases can provide names that better reflect the purpose or context of a type within a specific domain:

Kotlin also allows you to alias function types, which can make higher-order function signatures simpler and more expressive.

Finally, in projects where a specific type is used frequently in a particular context, a type alias can reduce repetition and potential errors in specifying that type:

By providing more descriptive names for complex or commonly used types, type aliases make the code easier to understand at a glance. They offer a flexible way to introduce readability improvements without the need for additional classes or interfaces.

Besides that type aliases are resolved at compile time and have no runtime overhead. However, they are not new types but alternative names to existing types. This means they do not create new types in the type system.

I hope you have enjoyed this tip! Don’t forget to subscribe and stay tuned for more Kotlin tips!

Stay curious!

Tip #24: Use lateinit for Late Initialization of Non-null Variables

Contribute

Writing takes time and effort. I love writing and sharing knowledge, but I also have bills to pay. If you like my work, please, consider donating through Buy Me a Coffee: https://www.buymeacoffee.com/RaphaelDeLio

Or by sending me BitCoin: 1HjG7pmghg3Z8RATH4aiUWr156BGafJ6Zw

Follow Me on Social Media

Stay connected and dive deeper into the world of Kotlin with me! Follow my journey across all major social platforms for exclusive content, tips, and discussions.

Twitter | LinkedIn | YouTube | Instagram

--

--

Kotlin with Raphael De Lio
Kotlin with Raphael De Lio

Published in Kotlin with Raphael De Lio

I’m Raphael, a software engineer with a passion for improving everything I touch. Born in Brazil and having lived in Portugal, I'm now a consultant in the Netherlands. At Xebia, I found a community sharing my enthusiasm for tech. I also organize the Amsterdam Kotlin MeetUp.

Raphael De Lio
Raphael De Lio

Written by Raphael De Lio

Software Consultant @ Xebia - Dutch Kotlin User Group Organizer: https://kotlin.nl