| J2CL JsInterop | Kotlin/JS | |
|---|---|---|
| Export a class/interface to JS | @JsType | @JsExport (top-level only) |
| Export a method to JS | @JsMethod | @JsExport (top-level only) |
| Export a field to JS | @JsProperty | @JsExport (top-level only) |
| Export a field to JS | @JsProperty | @JsExport (top-level only) |
| Don't export a symbol/method/field | @JsIgnore | @JsExport.Ignore |
| Specify a parameter as being optional | @JsOptional | Optional argument syntax |
| Specify a name for a symbol | name property on @JsType/@JsMethod/@JsProperty | @JsName |
| Specify the namespace to export a symbol to | A combination of the namespace and name from @JsType/@JsMethod/@JsProperty | Based on package statement, no additional customization allowed |
| Define a functional interface | @JsFunction | functional type expressions |
| J2CL JsInterop | Kotlin/JS | |
|---|---|---|
| Declare class/interface that exists in JS | @JsType(isNative = true) | external keyword |
| Declare method that exists in JS | @JsMethod with native keyword | external keyword |
| Declare field that exists in JS | @JsProperty with native keyword on a method, or just a @JsProperty in a isNative = true class. | Assign a property to definedExternally and/or mark the getter and/or setter as external |
| Specify a parameter as being optional | Add overloads omitting each of the optional arguments | Use the optional argument syntax assigning them to definedExternally |
| Specify a different name for a symbol | name property on @JsType/@JsMethod/@JsProperty | @JsName |
| Specify the module to import from | A combination of the namespace and name from @JsType/@JsMethod/@JsProperty | @JsModule if importing from a CommonJS module, or @JsQualifier if it exists in the global scope. |
| Define an overlay method on a JS class | @JsOverlay | Extension functions |
| Define a functional interface | @JsFunction | functional type expressions |
[!NOTE] Kotlin/JS interop semantics are experimental and may change over time. The analysis here is performed against Kotlin 2.1.0.
Kotlin/JS has a special dynamic type which can be used in situations where the type is unknown or cannot be represented in the Kotlin type system (ex. union types). Dynamic types can be passed as any type and can receive any type. They also do not perform any null checking.
Most interestingly, you can directly call functions (or access properties) on dynamic types that are otherwise never defined:
fun doSomething(d: dynamic) { // doSomethingElse is never defined in Kotlin d.doSomethingElse("foo") }
The call to doSomethingElse will be transpiled as-is, so it's just expected to be there at runtime. This is referred to as a dynamic call. They will always return the dynamic type.
J2CL doesn't have any direct comparable feature, but it does have similar capabilities. For example, a native JsType can be declared that has the type of ? in Closure to represent the unknown type. Unchecked casts can then be performed to go back and forth between that type.
Kotlin/JS performs monolithic compilation and will mangle the names of symbols/methods/properties. Therefore they have the @JsExport annotation to expose these to external callers. This annotation can be applied to any top-level class/interface, property, or function.
It is not allowed to be used on:
expect declarations@JsNameAnd declarations that are exported can only use “exportable” types:
Any, String, Boolean, Byte, Short, Int, Float, DoubleBooleanArray, ByteArray, ShortArray, IntArray, FloatArray, DoubleArrayArray<exportable-type>external or @JsExport classes and interfacesUnit return type. Must not be nullableKotlin has a concept of primary and secondary constructors as part of the language. Classes do not need to define a primary constructor, but if they do then all secondary constructors must delegate to it. Additionally, if a supertype has any constructor defined, then subtypes must delegate to any of the constructors.
When exporting a Kotlin type to JS, Kotlin will always assume there‘s a primary constructor. If it’s not explicitly defined, then it‘s implied to be empty. All secondary constructors become factory methods and all implicitly or explicitly delegate to the primary constructor. Because secondary constructors don’t have unique names, they must always be ignored or renamed with @JsName.
There‘s no additional checks when subtyping a @JsExport class with a primary constructor. Kotlin/JS does not use ES6 class syntax, rather they construct the prototype chains manually, so there’s no requirement that primary constructors of subtypes delegate to super primary constructors.
If a companion object's enclosing type is @JsExport'd, then the companion members are also considered exported. However, JS users will need to access the companion object via the Companion property on the enclosing type.
If users wish for some companion members to exist on the enclosing type as a static member in JS, the @JsStatic annotation can be used to do just that.
In general this is similar to the corresponding suite of @JsType, @JsProperty, and @JsMethod annotations.
Like @JsType, when @JsExport is applied to a class/interface, it applies to all public declarations. Unlike @JsType, it also applies to nested types recursively (though this can be undone with @JsExport.Ignore, similar to @JsIgnore).
Constructor behavior is quite a bit more flexible in Kotlin/JS since they do not transpile to ES6 class syntax which requires that constructors always call the super. While the idea of a primary constructor is similar to J2CL JsInterop‘s @JsConstructor, there’s no requirement for subtypes to delegate to supertypes' primary constructor.
Otherwise the restrictions on @JsExport are similar or reasonable for the additional syntax supported in Kotlin. One key difference is that enums cannot be exported, whereas in J2CL JsInterop they can be either via @JsType or @JsEnum. Additionally Kotlin only permits exporting top-level declarations, whereas J2CL JsInterop allows it at any level.
The restrictions on types used in exported declarations are similar to the J2CL JsInterop restrictions currently imposed, albeit they're only enforced as a warning in J2CL.
Exporting properties doesn‘t quite map to the equivalent @JsProperty as the latter permits a bit more flexibility in that it can be applied to a field or a method (to act as a getter/setter). In Kotlin @JsExport applied to a property will export the backing field if there’s no getter/setter, else a property in JS will be created with JS getter/setter wired to the Kotlin getter/setter.
Kotlin/JS utilizes the external keyword to mark top-level declarations as being defined external to the Kotlin compilation. Non top-level declarations can omit their implementation if they are enclosed in an external type, for example:
external class Foo { val bar: Int fun buzz(): Int }
There is also a special definedExternally property that can optionally be used as a marker for an externally implemented property/function. This is only required when marking an optional parameter in an externally defined function, for example:
external class Foo { val bar: Int = definedExternally // this assignment isn't required fun buzz(optionalParam: Int = definedExternally): Int }
Unlike the checks for @JsExport, external members do not have their referenced types checked to ensure that they're exported or external types.
While external interfaces are supported, it does come with additional restrictions, namely that they cannot be used in an is expression, or used to special a reified type. Additionally, any type cast will always succeed.
In J2CL we have the concept of a @JsOverlay annotated method which allows non-native methods to be added a native type. This is particularly useful for adding bridge methods that don't actually exist on the underlying type. Any virtual methods are devirtualized and take the receiver as a parameter. Due to this there are limitations on these methods, namely that they cannot be polymorphic.
Kotlin supports this as a language feature for any type via extension functions/properties with the same limitations that @JsOverlay has.
Kotlin/JS allows defining as many secondary constructors as desired in external types. If there‘s a primary constructor, secondary constructors do not need to delegate to it (as that’s assumed to be part of the body of the constructor). Kotlin/JS considers all primary and secondary constructors to be the same constructor in JS.
Subtypes are free to delegate to any of the constructors.
Since Kotlin doesn't have static members, all companion members with an enclosing external type will be considered as being static members on the underlying type. That is, to represent the JS code:
class Foo { static bar() { console.log("hi there!"); } }
You would write in Kotlin/JS:
external class Foo { companion object { fun bar() } }
Kotlin/JS's external syntax is very similar to J2CL JsInterop behavior for isNative = true and native marked methods, but nicely baked into the language itself. The restrictions on external types are also similar, wherein interface types are supported, but with additional restrictions on how the type can be used (i.e. instanceof checks).
In fact, many of these features are already supported in Kotlin/Closure as we needed to use the external syntax as an equivalent for Java's native syntax.
There are cases where both Kotlin/JS and J2CL developers need to handwrite an implementation that should be used as-is. In J2CL this is done by declaring native methods on non-native enclosing types. It is then expected that the implementation of the method will be provided in an adjacent native.js file.
In Kotlin/JS there are two ways to do this. One way is by using dynamic types and dynamic calls (as covered earlier), or via the js function which accepts a string literal containing JS code that will be used as-is. For example:
class Foo { fun random(from: Int, to: Int): Int { return js("(Math.random() * (to - from + 1) + from) | 0") } }
Neither Kotlin/JS nor J2CL does any checking of this code, so it's entirely on the user to accurately represent the types.
While Kotlin does have SAM functions (declared with the fun interface syntax), it does not provide interop between Kotlin/JS. All implementations of these SAM interfaces will be a real class with a real method rather than just a bare function.
However, a functional type can be used in place of a fun interface type to replicate the behavior of accepting a bare JS function:
@JsExport fun foo(f: () -> Unit) = f()
which will transpile to:
function foo(f) { f(); }
This means there‘s no direct equivalent to J2CL JsInterop’s @JsFunction feature as you cannot declare an interface type that can be reused, however, using a functional type does serve much of the same purpose. Additionally, functional types are likely going to be more desirable to users as @JsFunction interfaces don't serve much purpose in Java.
Kotlin does support limited instanceof checks on functional types, namely you can perform the following checks:
foo is () -> Unitfoo is Function0<Unit>foo is Function0<*>Any other instanceof check will fail to compile. All of those examples will transpile to typeof foo == 'function', so any function will pass the check.
Since enums don't actually exist in the JS language, Kotlin/JS only supports exporting Kotlin-defined enums to JS. Declaring an external enum is not supported.
J2CL supports exporting an enum type with @JsType, which mimics the Kotlin/JS behavior. It also supports interoperability with Closure enum types when using the @JsEnum annotation. There is no equivalent behavior in Kotlin/JS.