Should I use `enum` or an `interface` with inline ...
# javascript
h
Should I use
enum
or an
interface
with inline functions and
unsafeCast
to reduce binary size?
t
What is your use case?
h
I want to provide a typed API, so I want to limit the usage using enums.
Copy code
public enum class Color(private val value: String) {
    Primary("primary"),
    Secondary("secondary"),
    Success("success"),
    Info("info"),
    Warning("warning"),
    Danger("danger"),
    Light("light"),
    Dark("dark")
}
Alternative:
Copy code
public interface Color {
    public companion object Theme {
        public inline val Primary: Color get() = Color("primary")
        public inline val Secondary: Color get() = Color("secondary")
        public inline val Success: Color get() = Color("success")
        public inline val Danger: Color get() = Color("danger")
        public inline val Warning: Color get() = Color("warning")
        public inline val Info: Color get() = Color("info")
        public inline val Light: Color get() = Color("light")
        public inline val Dark: Color get() = Color("dark")
    }
}

public inline fun Color.background(): Color = Color("bg-${unsafeCast<String>()}")
public inline fun Color(value: String): Color = value.unsafeCast<Color>()
the second approach was provided by Oleksandr: https://github.com/JetBrains/compose-jb/pull/859#issuecomment-875737202 but does this really result in smaller code? It's definition is uncommon and not so handy.
Lightweight CSS types you can find here: https://github.com/JetBrains/kotlin-wrappers/tree/master/kotlin-csstype cc @Oleksandr Karpovich [JB]
h
Sorry, could you please explain it?
t
Which example?
AutoComplete
?
h
Everything 😄 For classes already defined in JS standard, like `AutoComplete, you use external enum, which sounds nice. What about non defined things?
And many packages in
kotlin wrapper
includes some docs from
react
. Are all packages without
react
in its name safe to use without react? I dont want to include react only for useful! types 🙂
t
csstype
- common library
AutoComplete
and all other enums, which emulate string unions are also common
csstype
can be used as dependency of Compose
“Unions” from react packages must be copied/adopted
h
I created a ticket to integrate some wrapper in compose web :) https://github.com/JetBrains/compose-jb/issues/1433
a
Is it possible to expose something like an
external enum class
as a Typescript
enum
instead of a Union type?
1
t
Example required