I cannot extend kotlin.Number

I want to implement a BigInteger class like

class BigInteger internal constructor(val bigInt: dynamic): Number(), Comparable<BigInteger> {
    ......
    
    override fun toInt(): Int {
        val myBigInt = this.bigInt
        return js("Number(BigInt.asIntN(32, myBigInt))") as Int
    }

    override fun toLong(): Long {
        val myBigInt = this.bigInt
        return js("Kotlin").Long.fromBits(
                this.toInt(), js("Number(BigInt.asIntN(64, myBigInt) >> BigInt(32))")) as Long
    }

    override fun compareTo(other: BigInteger): Int {
        val myBigInt = this.bigInt
        val otherBigInt = other.bigInt
        return js("(myBigInt > otherBigInt ? 1 : (myBigInt < otherBigInt ? -1 : 0))") as Int
    }
}

But when I run the unit test, I got an error:

TypeError: Cannot read properties of undefined (reading ‘prototype’)
at Object. (C:/Users/firas/AppData/Local/Temp/_karma_webpack_187172/commons.js:202:49)

The generated code at commons.js:202 is

BigInteger.prototype = Object.create(Number_0.prototype);

And Number_0 is defined as

var Number_0 = Kotlin.kotlin.Number;

In JS Number is intrinsic, transformed to a platform number, so it obviously could not be extended.

In general, extending Number is a bad practice since it does not give you anything, but a headache.

1 Like