<@U33H6SB2B> with the new changes to typed arrays ...
# javascript
e
@turansky with the new changes to typed arrays in kotlin-wrappers, I'm stuck at trying to find a solution to my typealiasing. See https://youtrack.jetbrains.com/issue/KT-60561/K2-cant-instantiate-expect-class-with-an-actual-class-that-has-type-arguments#focus=Comm[…]1204608.0-0 Basically I'm currently using
Copy code
// commonMain
public expect class ZByteArray(size: Int)

// jsMain
public actual typealias ZByteArray = Uint8Array
However, with the new types it is required to specify a generic parameter
Copy code
// jsMain
public actual typealias ZByteArray = Uint8Array<ArrayBufferLike>
Which then doesn't compile anymore. The
ZByteArray
typealias is then used in exported classes/interfaces. For example
Copy code
@JsExport
public interface ZOutputStream {
  public fun writeBytes(bytes: ZByteArray)
}
and that means subclassing
Uint8Array<ArrayBufferLike>
isn't a solution. Note that while the kotlin-wrappers way emulates TypeScript, TypeScript also uses a default generic type, which isn't possible in Kotlin.
Copy code
interface Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>
t
As workaround:
Copy code
// jsMain
@JsName("Uint8Array")
public actual open external class ZByteArray(size: Int) : Uint8Array<ArrayBufferLike>
?
Or probably better solution - value class
e
Copy code
public actual open external class ZByteArray
this version is then incompatible with other kotlin-wrappers types like
Buffer
Copy code
sealed external class Buffer : Uint8Array<ArrayBufferLike>
Managed to get it to compile with a bunch of
unsafeCast<Buffer>
and
unsafeCast<ZByteArray>
. Not ideal but it's a first step.
t
value class - isn't variant?
e
Mmm but how would I
@JsExport
signatures that use the value class?
What about adding additional types like (name is just a guess)
Copy code
@JsName("Uint8Array")
external class UUint8Array : Uint8Array<ArrayBufferLike>
Than
UUint8Array
could replace all usages of
Uint8Array<ArrayBufferLike>
to mimic the default generic type.
t
image.png
Additional local type as workaround - my initial suggestion, which you rejected ;)
e
That's what I'm using as of now, with a bunch of
unsafeCast
. Since typed arrays are also used under kotlin-nodes types, every interaction with them requires an
unsafeCast
.