Alternative to @nativeGetter


#1

I have a use case that seems to require @nativeGetter, but that annotation is deprecated.

I have this:

external class LoaderResults {
    @nativeGetter operator fun get(resource: String): LoaderResult?
}

interface LoaderResult {
    val name: String
    val error: Error?
}

What’s the recommended non-deprecated approach?


How to avoid this surprising Map behavior with external JS classes
#2

I believe it should be:

external class LoaderResults
inline fun LoaderResults.get(resource: String): LoaderResults? = asDynamic().get(resource)

See https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/native-getter/index.html


#3

You also have to add operator modifier and use asDynamic()[resource] instead of asDynamic().get(resource)

external class LoaderResults
inline operator fun LoaderResults.get(resource: String): LoaderResults? = asDynamic()[resource]