Hello, I have a `sealed class` with a `companion o...
# javascript
h
Hello, I have a
sealed class
with a
companion object
that has some fields and functions that access fields in the declared
object
and inner
sealed class
. In JVM it compiles fine, but in KotlinJS it says that it cannot resolve the fields. In this example the Error
Unresolved reference 'name'
would be at the
it.name
Copy code
sealed class Product(open val name: String) {
    object TestProduct1 : Product("Test1")

    sealed class MetaProduct(override val name): Product(name) {
        object SubProduct1: MetaProduct("sub1")
        object SubProduct2: MetaProduct("sub2")
    }

    companion object {
        val entries = listOf(
            TestProduct1,
            MetaProduct.SubProduct1,
            MetaProduct.SubProduct2
        )
        val byName = entries.associateBy { it.name } // Error here in KotlinJS, but not JVM
    }
}
e
h
I have the solution, I just needed to specify the type on the
listOf
to
listOf<Product>
Ah thank you ephemient!