Is there anyway we can generate Typescript union t...
# javascript
s
Is there anyway we can generate Typescript union types in js export? Expected it would work with sealed classes but didn’t If its possible or planned, lmk, as it would be a much nicer experience for downstream js devs.
a
Hi @shikhar kodee welcoming . We don't have such plans yet, but I'm curious. Could you please describe, how it would ideally look?
s
Well I tried writing an example which went along the lines of Something like this:
Copy code
sealed interface Event {
    data object SomethingHappened : Event
    data object SomethingElseHappened : Event
    data class SomethingHappenedToSomeone(val someone: String) : Event
}
Generates:
Copy code
export type SomethingHappened = {};
export type SomethingElseHappened = {};
export type SomethingHappenedToSomeone = { someone: string };

export type Event =
  | SomethingHappened
  | SomethingElseHappened
  | SomethingHappenedToSomeone;
… and realised ts types just don’t map with anything in kotlin to interoperate cleanly just for context, my usecase was library users could write exhaustive error handling so do something like:
fun someApi(): Result<SomeApiResp, SomeApiRespErrors>
where SomeApiRespError has well-defined exhaustive subtypes
a
It totally makes sense. Thank you for sharing. There is a similar issue: https://youtrack.jetbrains.com/issue/KT-71798/KJS-Sealed-classes-should-be-compiled-to-union-types You can subscribe to it for updates regarding any related work