Hi this is my first time using the slack... so I'm...
# javascript
i
Hi this is my first time using the slack... so I'm sorry if I'm not doing it right or broke some universal slack rules.... I'm finishing up making my own TS declaration to Kt external converter... On the topic of working with Union types using Kotlin, do we think this would work, and if so, be a decent union type approach?
Copy code
external class JavascriptComponent // -- properly defined elsewhere

// declare var Root : JavascriptComponent | any;

external var Root: RootUnion

sealed external class RootUnion

fun RootUnion.asJavascriptComponent(): JavascriptComponent = asDynamic().unsafeCast<JavascriptComponent>()
fun RootUnion.asAny(): Any = asDynamic().unsafeCast<Any>()
a
yes, that approach makes sense to me
using an interface might be better than a class
Copy code
sealed external interface RootUnion
since an external interface doesn’t exist
I’m sorry if I’m not doing it right or broke some universal slack rules....
You picked the right channel and you’re on topic blob smile. But could you move your other message into this thread 🧵, so the conversation is grouped?
i
Thank you 😄 I just now found these replies lol
👍 1
another approach I was considering trying is :
Copy code
@JsName("Root")
external var ExternalRoot: dynamic

val Root by lazy { RootUnion(ExternalRoot) }


class RootUnion(private val input: dynamic) {
    fun asJavascriptComponent() = input.unsafeCast<JavascriptComponent>()
    fun asAny() = input.unsafeCast<Any>()
}
or
Copy code
external var Root : dynamic
val RootJavascriptComponent get() = Root.unsafeCast<JavascriptComponent>()
val RootAny get() = Root.unsafeCast<Any>()
Oooh okay, I see.. i like the external interface idea, I will try going with that one!
a
the second approach might also work, but using a
lazy {}
delegate might be a problem since there’s a performance costs with delegates
i
If the first way seems viable i think i like it best 😄