I’ve recently spent some time with TypeScript and ...
# announcements
a
I’ve recently spent some time with TypeScript and enjoyed its type unions enough that I wondered whether Kotlin could do the same. Initially the answer seemed to be no, but a blog post about multiple constraints on generics led me to a solution which kinda seems to be equivalent:
Copy code
interface A {
    fun getInt(): Int
}

interface B {
    fun getString(): String
}

fun <Union> kotlinTypeUnion(param: Union) where Union: A, Union: B {
    val fromA: Int = param.getInt()
    val fromB: String = param.getString()
}

kotlinTypeUnion(object: A, B {
    override fun getInt() = 1
    override fun getString() = "two"
})
any thoughts?