andylamax
02/09/2022, 11:29 PMsealed class WatchMode {
companion object {
val DEFAULT: WatchMode = CASUALLY
}
}
object EAGERLY : WatchMode()
object CASUALLY : WatchMode()
Generates the following type definitions
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
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
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?Big Chungus
02/10/2022, 12:41 AMandylamax
02/10/2022, 12:54 AM@JvmStatic
equivalents for JS. My current work around looks like this
commonMain/WatchMode.kt
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
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
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/TSshaktiman_droid
03/02/2022, 4:27 PMsealed
types as well
// commonMain
enum Gender {
MALE,
FEMALE
}
and in jsMain
@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