At the moment, ```sealed class WatchMode { com...
# javascript
a
At the moment,
Copy code
sealed class WatchMode {
    companion object {
        val DEFAULT: WatchMode = CASUALLY
    }
}

object EAGERLY : WatchMode()

object CASUALLY : WatchMode()
Generates the following type definitions
Copy code
class WatchMode {
    private constructor();
    readonly Companion: {
        readonly DEFAULT: live.WatchMode;
    };
}
const EAGERLY: {
} & live.WatchMode;
const CASUALLY: {
} & live.WatchMode;
This makes it impossible to get the value of
WatchMode.DEFAULT
from js/typescript impossible, because one would need to create an instance of
WatchMode
(whose constructor is private) and then get the
DEFAULT
property from
Companion
. Why is it a property, can't it be defined on
WatchMode
instead of it's
prototype
? N;B. When the hierarchy is defined as show bellow
Copy code
sealed class WatchMode {
    companion object {
        val DEFAULT: WatchMode = CASUALLY
    }

    object EAGERLY : WatchMode()

    object CASUALLY : WatchMode()
}
The generated types are still unsable from js/typescript code. Funny thing is, classes work like a
Copy code
class WatchMode {
    private constructor();
    readonly Companion: {
        readonly DEFAULT: live.WatchMode;
    };
    readonly EAGERLY: {
    } & live.WatchMode;
    readonly CASUALLY: {
    } & live.WatchMode;
}
Is there a known workaround for this? or a reported ticket somehow?
b
See if there is something like @JvmStatic for js. Alternatively, do not export kotlin types to js and instead provide external js types
a
There are no
@JvmStatic
equivalents for JS. My current work around looks like this commonMain/WatchMode.kt
Copy code
sealed class WatchMode {
    companion object {
        val CASUALLY by lazy { CASUALLY() }
        val EAGERLY by lazy { EAGERLY() }
        val DEFAULT: WatchMode by lazy { CASUALLY }
    }
}

class EAGERLY internal constructor() : WatchMode()

class CASUALLY internal constructor() : WatchMode()
jsMain/WatchModeJs.kt
Copy code
val CASUAL_WATCH_MODE by lazy { WatchMode.CASUALLY }
val EAGER_WATCH_MODE by lazy { WatchMode.EAGERLY }
val DEFAULT_WATCH_MODE by lazy { WatchMode.DEFAULT }
This workaround, generates the following types
Copy code
class WatchMode {
    private constructor();
    readonly Companion: {
        readonly CASUALLY: live.CASUALLY;
        readonly EAGERLY: live.EAGERLY;
        readonly DEFAULT: live.WatchMode;
    };
}
class EAGERLY extends live.WatchMode {
    private constructor();
}
class CASUALLY extends live.WatchMode {
    private constructor();
}
const CASUAL_WATCH_MODE: CASUALLY;
const EAGER_WATCH_MODE: EAGERLY;
const DEFAULT_WATCH_MODE: WatchMode;
Which are fairly usable in JS/TS
s
For enum we do this workaround which should work for
sealed
types as well
Copy code
// commonMain

enum Gender {
    MALE,
    FEMALE
}
and in
jsMain
Copy code
@Suppress("NON_EXPORTABLE_TYPE")
@ExperimentalJsExport
@JsExport
object GenderType {
    val male = Gender.MALE
    val female = Gender.FEMALE
}
Then
GenderType.male
is usable on JS side and can be passed to a function on kotlin side that accepts
Gender
enum