How to add @JsName to a platform function

You could do something like this:

// Common module
expect annotation class PlatformName(val name: String)

class PrettyPrinter {
    @PlatformName("jsonToString")
    fun jsonToString...
}

// JS module
actual typealias PlatformName = JsName

// JVM module
// Either create some dummy annotation, or alias to JvmName

This way PlatformName will be transformed to JsName when compiling to JS.

Overall using platform-specific annotations in common code is a bit of a pain. Most likely there will be some simple solution when MPP is released (it is experimental at the moment).

2 Likes