[maybe a bug] static method in kotlinJs don't work

Hello, I’m making a multiplatform java/js lib

I have issue to use static method in kotlin to js : They are not added to the class

my code:

package mu;

class A {
    fun aa() = "aa"

class B {
    fun bb() = "bb"

    companion object {
        fun cc() = "cc"

the compile:

(function (_, Kotlin) {
  'use strict';
  var Kind_CLASS = Kotlin.Kind.CLASS;
  var Kind_OBJECT = Kotlin.Kind.OBJECT;
  function A() {
  A.prototype.aa = function () {
    return 'aa';
  A.$metadata$ = {
    kind: Kind_CLASS,
    simpleName: 'A',
    interfaces: []
  function B() {
  B.prototype.bb = function () {
    return 'bb';
  function B$Companion() {
    B$Companion_instance = this;
  B$Companion.prototype.cc = function () {
    return 'cc';
  B$Companion.$metadata$ = {
    kind: Kind_OBJECT,
    simpleName: 'Companion',
    interfaces: []
  var B$Companion_instance = null;
  function B$Companion_getInstance() {
    if (B$Companion_instance === null) {
      new B$Companion();
    return B$Companion_instance;
  B.$metadata$ = {
    kind: Kind_CLASS,
    simpleName: 'B',
    interfaces: []
  var package$mu = _.mu || (_.mu = {});
  package$mu.A = A;
  Object.defineProperty(B, 'Companion', {
    get: B$Companion_getInstance
  package$mu.B = B;
  Kotlin.defineModule('M1', _);
  return _;
}(module.exports, require('kotlin')));

//# sourceMappingURL=M1.js.map

The issue:
new AA().aa is defined
new BB().bb in defined
BB.cc is undefined ( and i need it)

can you help me?

If you are calling BB.cc from JavaScript directly, not from Kotlin/Js, you’ll have to use the Companion object just like in Java: BB.Companion.cc

Also, note that cc is not a static method of BB because there is not such thing as static in Kotlin.

Check: https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects

1 Like

the Kotlin “compiler” build the call to method in other external lib that way, without the companion

Why would you need to use BB.cc within js? Why can’t you use BB.Companion.cc?

Further explanation:

Neither Kotlin nor JS know the concept of static functions.

When you write BB.cc in Kotlin, it is syntactic sugar. Kotlin will automatically use BB.Companion.cc instead.

JS on the other hand, does not know about this syntactic sugar, that’s why BB.cc will not work in JS.

That… would explain a lot of my issue. Thanks.

also, I have a similare issue but not with a class, with a :

expect object EE {
fun FF()


in js

actual object EE {
actual fun FF(){ FFjs()}

in java

actual object EE {
actual fun FF(){ FFjava()}

Should I access the FF method by Companion too?

You are using this argument incorrectly. Just because there are generics in Kotlin, does not mean that you can provide actual type arguments when calling Kotlin functions from within JS code.

Indeed, and it does! What you are complaining about does not work in Java neither: you cannot use BB.cc from Java code.