Mobile Development Kotlin Subjective
Oct 04, 2025

What are type aliases and when should you use them?

Detailed Explanation
Type aliases create alternative names for existing types, improving code readability and maintainability.\n\n**Basic type aliases:**\n\ntypealias UserId = Int\ntypealias UserName = String\ntypealias EmailAddress = String\n\nfun createUser(id: UserId, name: UserName, email: EmailAddress) {\n // Implementation\n}\n\n// Usage\nval userId: UserId = 123\nval userName: UserName = "John Doe"\nval email: EmailAddress = "john@example.com"\n\n\n**Function type aliases:**\n\ntypealias EventHandler = (String) -> Unit\ntypealias Predicate = (T) -> Boolean\ntypealias Transformer = (T) -> R\n\nclass EventManager {\n private val handlers = mutableListOf()\n \n fun addHandler(handler: EventHandler) {\n handlers.add(handler)\n }\n \n fun fireEvent(event: String) {\n handlers.forEach { it(event) }\n }\n}\n\n// Usage\nval logger: EventHandler = { event -> println("Log: $event") }\nval emailer: EventHandler = { event -> sendEmail(event) }\n\n\n**Generic type aliases:**\n\ntypealias StringMap = Map\ntypealias MutableStringMap = MutableMap\ntypealias NodeList = List>\n\n// Usage\nval userPreferences: StringMap = mapOf(\n "theme" to "dark",\n "language" to "en"\n)\n\nval cache: MutableStringMap = mutableMapOf()\n\n\n**Complex type aliases:**\n\ntypealias ApiResponse = Result\ntypealias ValidationResult = Either, ValidatedData>\ntypealias DatabaseConnection = Connection\n\n// Nested generics\ntypealias UserRepository = Repository\ntypealias AsyncResult = CompletableFuture>\n\n\n**Class and interface aliases:**\n\ntypealias MyList = ArrayList\ntypealias StringProcessor = Function\n\nclass DataProcessor {\n private val items: MyList = MyList()\n private val processor: StringProcessor = { it.uppercase() }\n}\n\n\n**When to use type aliases:**\n\n**✅ Good use cases:**\n• Simplifying complex generic types\n• Creating domain-specific names for primitive types\n• Shortening frequently used function types\n• Improving API readability\n• Reducing repetition in type declarations\n\n**❌ Avoid when:**\n• You need type safety (use value classes instead)\n• The alias doesn't add clarity\n• It's used only once or twice\n• It makes code less clear\n\n**Type aliases vs Value classes:**\n\n// Type alias - no runtime type safety\ntypealias UserId = Int\nfun getUser(id: UserId): User = TODO()\nfun getOrder(id: Int): Order = TODO()\n\nval userId: UserId = 123\ngetOrder(userId) // Compiles but wrong!\n\n// Value class - runtime type safety\n@JvmInline\nvalue class UserId(val value: Int)\n\nval userId = UserId(123)\n// getOrder(userId) // Compilation error!\n\n\n**Nested type aliases:**\n\nclass OuterClass {\n typealias InnerAlias = String\n \n fun process(value: InnerAlias) {\n println(value)\n }\n}\n\n// Access nested alias\ntypealias GlobalInnerAlias = OuterClass.InnerAlias\n\n\n**Import aliases:**\n\nimport com.example.ui.Button as UiButton\nimport com.example.data.Button as DataButton\n\n// Now can use both without conflict\nval uiButton = UiButton()\nval dataButton = DataButton()\n
Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback