With inline classes being supported in the new IR ...
# javascript
a
With inline classes being supported in the new IR for JS, is there a possibility for inline enum classes to be prioritized? https://youtrack.jetbrains.com/issue/KT-23823 TS “enums” are usually typed as unions (
type MyEnum = "a" | "b" | "c"
). Having
inline enum class
support (especially with generates TS definitions) would help greatly with calling Kotlin/JS code from Typescript. EDIT: it looks like there are a few other ways to represent Enums in TS: • enum (
enum MyEnum2 { A = "a", B = "b", C = "c")
) • const enum (
const enum MyEnum2 { A = "a", B = "b", C = "c")
) I want proper mapping between Kotlin/TS enums -- inline classes and/or union types are just one way I see to get that
g
a possibility for inline enum classes to be prioritized
This is not JS-only feature so it should be discussed in context of all platforms, to prioritize it, we at least need KEEP for it, now it’s just an idea raised during discussion of inline classxes
👍 2
Also I’m not sure that support of union types related on this feature
a
Gotcha. I think I was under-informed about the different way of representing "enums" in TS and jumped to
inline enum class
as the ideal solution. It looks like Typescript supports: • string union (
type MyEnum1 = "a" | "b" | "c"
) • enum (
enum MyEnum2 { A = "a", B = "b", C = "c")
) • const enum (
const enum MyEnum2 { A = "a", B = "b", C = "c")
) Do Kotlin `enum class`es get mapped to/from any of these in the new IR's .d.ts generation?
s
Kotlin enum classes are mapped to real objects: entries can have member functions and properties, they can implement interfaces, etc. TS export of enums is currently prohibited because it is not designed yet. But it we were to export them as they are implemented internally, it would look like:
Copy code
type MyEnum4 = { $name: 'A', /* ... */ } | { $name: 'B', /* ... */ }
a
Got it. Is there a youtrack ticket for more lightweight enums that can be transparently used as enums from TS? That seems like something that implies support for `inline enum class`es, yeah?
s
I don’t know if there is an issue specifically for that. Multiplatform
inline enum class
es are definitely a good fit for JS export. But, I guess, they can be just enum classes with
@JsExport
on JS platform only.
s
Unions would enable ad-hoc polymorphism, which has benefits but I’m not sure how high a priority it is. Discussed to death here: https://discuss.kotlinlang.org/t/extension-types-for-kotlin/1390/24
a
Sorry, I didn't mean to sidetrack into union types or inline enum classes in particular -- I just assumed that was the best way to do what i'm trying to do. The specific problem I'm trying to solve is using Kotlin enums as regular Typescript enums when interacting with Kotlin/JS code from TS. It looks like the main sticking point right now is that Kotlin enum classes are more heavyweight than TS enums, so they can't directly be mapped to them. I'm not sure that a
@JsExport
on a heavyweight Kotlin enum class would be able to cleanly map into a TS
enum
, but I'd definitely be in favor of that if possible 🙂