Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A type alias in Kotlin provides an alternative name for an existing type. It does not introduce a new type into the type system; rather, it acts as a compile-time substitution. During compilation, the Kotlin compiler replaces the alias with its underlying target type, meaning the alias and the original type are strictly identical and interchangeable in the generated bytecode.

Syntax

Type aliases are declared using the typealias keyword, followed by the new identifier, an assignment operator, and the target type.
typealias AliasName = TargetType

Technical Characteristics

  • Top-Level Declaration: Type aliases must be declared at the top level of a file. They cannot be nested inside classes, objects, interfaces, or local scopes (functions).
  • Type Identity: Because a type alias is not a distinct type, an instance of the alias can be assigned to a variable of the original type, and vice versa, without any type casting or conversion.
  • Visibility Modifiers: Type aliases support standard visibility modifiers (public, private, internal). A type alias cannot have a more permissive visibility than its underlying target type.

Structural Mechanics

1. Standard Type Substitution A direct mapping to an existing class, interface, or parameterized type.
typealias StringMap = Map<String, String>

val headers: StringMap = mapOf("Accept" to "application/json")
val standardMap: Map<String, String> = headers // Strictly identical, no cast required
2. Function Type Aliasing Type aliases can represent function signatures, including those with receivers, multiple parameters, or specific return types.
typealias CoordinatesHandler = (x: Int, y: Int) -> Unit
typealias Transformer<T, R> = (T) -> R
3. Generic Type Parameterization Type aliases can declare their own generic type parameters, which are then applied to the underlying target type.
typealias Matrix<T> = List<List<T>>
typealias Dictionary<V> = Map<String, V>

// Instantiation applies the generic type to the underlying Map
val dict: Dictionary<Int> = mapOf("count" to 1) 
4. Nested and Inner Class Aliasing Type aliases can resolve fully qualified paths to nested or inner classes at compile time.
class NetworkManager {
    inner class Connection
}

typealias NetConnection = NetworkManager.Connection

Reflection and Type Checking

Because type aliases are resolved during compilation, runtime type checks (is operator) and reflection evaluate the underlying type, not the alias. Class literals using the alias are fully supported by the compiler and evaluate directly to the class literal of the underlying target type.
typealias AuthToken = String

val token: AuthToken = "abc-123"

// Evaluates to true because AuthToken is compiled as String
println(token is String) 

// Valid syntax: AuthToken::class evaluates to String::class
println(AuthToken::class == String::class) // Prints: true
Master Kotlin with Deep Grasping Methodology!Learn More