-
Notifications
You must be signed in to change notification settings - Fork 22
Kotlin: Type Alias
Devrath edited this page Dec 25, 2023
·
3 revisions
- Sometimes when we use a particular keyword, class, or any type in many places, There are scenarios we want it to be more readable.
- Just to make it readable we don't have to push it to another custom class because it will change the type.
- Kotlin provides
type-alias
, by using it we can give our name to thetype
without altering it. - As seen in the example below, We gave AuthToken and we can directly use it and the type remains
String
Declaring TypeAlias
typealias AuthToken = String
Usage of TypeAlias
class TypeAliasVm @Inject constructor( ) : ViewModel() {
data class Student(val name : String, val age : Int, val authTolken : AuthToken)
data class Teacher(val name : String, val age : Int, val authTolken : AuthToken)
fun demo() {
val dataOne = Student(name = "Mahesh", age = 20 , authTolken = "282828fhisb")
val dataTwo = Teacher(name = "Suresh", age = 40 , authTolken = "282828fhisb")
println(dataOne.authTolken)
println(dataTwo.authTolken)
}
}