Kotlin Standard Library for JavaScript target with comprehensive W3C DOM bindings and JavaScript interoperability features.
npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-js@2.2.0The Kotlin Standard Library for JavaScript provides JavaScript-specific extensions to the Kotlin standard library, enabling developers to write Kotlin code that compiles to JavaScript while maintaining full interoperability with existing JavaScript ecosystems. It includes comprehensive type-safe bindings for W3C web standards, JavaScript-specific collection implementations, platform-specific I/O operations, and essential runtime support for Kotlin language features in JavaScript environments.
implementation("org.jetbrains.kotlin:kotlin-stdlib-js:2.2.0")// Core JavaScript interop
import kotlin.js.*
// DOM and browser APIs (W3C bindings included in kotlin-stdlib-js)
import org.w3c.dom.*
import org.w3c.xhr.*
import org.w3c.files.*
// Collections
import kotlin.collections.*
// Coroutines
import kotlin.coroutines.*
// Reflection
import kotlin.reflect.*Note:
kotlinx.browser.* and kotlinx.dom.* require additional dependencies and are not part of kotlin-stdlib-js. The kotlin.browser.* package in stdlib-js contains deprecated APIs.kotlin.uuid.* are experimental and require @OptIn(ExperimentalUuidApi::class) to use.import kotlin.js.*
import org.w3c.dom.*
// JavaScript interop
val result = js("Math.max(1, 2, 3)") // Execute JavaScript code
val jsonObj = json("name" to "John", "age" to 30) // Create JSON objects
// JSON operations
val jsonString = JSON.stringify(jsonObj)
val parsed = JSON.parse<dynamic>(jsonString)
// Type-safe collections
val list = arrayListOf("apple", "banana", "orange")
val map = hashMapOf("key1" to "value1", "key2" to "value2")
// Promises and async operations
val promise = Promise.resolve("Success")
promise.then { result ->
js("console.log('Promise resolved: ' + result)")
}
// Dynamic type operations
val anyValue: Any = "test"
val dynamicValue = anyValue.asDynamic()
val typeString = jsTypeOf(anyValue) // Returns "string"The Kotlin Standard Library for JavaScript is built around several key components:
Core JavaScript interop functionality for seamless integration with JavaScript code, including dynamic typing, JSON manipulation, and Promise support.
// Execute JavaScript code inline
external fun js(code: String): dynamic
// JavaScript type checking
external fun jsTypeOf(a: Any?): String
// External declarations placeholder
external val definedExternally: Nothing
// Dynamic type casting
inline fun Any?.asDynamic(): dynamic
inline fun <T> Any?.unsafeCast(): T
// JSON support
external interface Json {
operator fun get(propertyName: String): Any?
operator fun set(propertyName: String, value: Any?)
}
fun json(vararg pairs: Pair<String, Any?>): Json
fun Json.add(other: Json): Json
external object JSON {
fun stringify(o: Any?): String
fun stringify(o: Any?, replacer: ((key: String, value: Any?) -> Any?)): String
fun stringify(o: Any?, replacer: ((key: String, value: Any?) -> Any?)?, space: Int): String
fun stringify(o: Any?, replacer: ((key: String, value: Any?) -> Any?)?, space: String): String
fun stringify(o: Any?, replacer: Array<String>): String
fun stringify(o: Any?, replacer: Array<String>, space: Int): String
fun stringify(o: Any?, replacer: Array<String>, space: String): String
fun <T> parse(text: String): T
fun <T> parse(text: String, reviver: ((key: String, value: Any?) -> Any?)): T
}
// Promise support
external class Promise<out T>(executor: (resolve: (T) -> Unit, reject: (Throwable) -> Unit) -> Unit) {
fun <S> then(onFulfilled: ((T) -> S)?): Promise<S>
fun <S> then(onFulfilled: ((T) -> S)?, onRejected: ((Throwable) -> S)?): Promise<S>
fun <S> catch(onRejected: (Throwable) -> S): Promise<S>
fun finally(onFinally: () -> Unit): Promise<T>
companion object {
fun <S> all(promise: Array<out Promise<S>>): Promise<Array<out S>>
fun <S> race(promise: Array<out Promise<S>>): Promise<S>
fun reject(e: Throwable): Promise<Nothing>
fun <S> resolve(e: S): Promise<S>
fun <S> resolve(e: Promise<S>): Promise<S>
}
}Comprehensive type-safe bindings for W3C web standards including DOM manipulation, HTTP requests, file handling, media APIs, and WebGL.
// Core DOM interfaces
external interface Document : Node
external interface Element : Node
external interface HTMLElement : Element
// Event handling
external interface Event
external interface EventTarget {
fun addEventListener(type: String, listener: EventListener)
}
// HTTP requests
external fun fetch(input: String, init: RequestInit? = definedExternally): Promise<Response>
external interface Response {
val ok: Boolean
val status: Short
fun text(): Promise<String>
fun json(): Promise<dynamic>
}
// File API
external interface File : Blob {
val name: String
val lastModified: Number
}
external interface FileReader : EventTarget {
fun readAsText(file: Blob)
val result: String?
}JavaScript-optimized implementations of Kotlin collection interfaces with specialized performance characteristics for JavaScript runtime.
// Mutable collection implementations
actual class ArrayList<E> : AbstractMutableList<E>
actual class HashMap<K, V> : AbstractMutableMap<K, V>
actual class HashSet<E> : AbstractMutableSet<E>
actual class LinkedHashMap<K, V> : AbstractMutableMap<K, V>
actual class LinkedHashSet<E> : AbstractMutableSet<E>
// String-optimized collections
fun <V> stringMapOf(vararg pairs: Pair<String, V>): MutableMap<String, V>
fun stringSetOf(vararg elements: String): MutableSet<String>JavaScript-specific coroutine support with generator-based execution and event loop integration for asynchronous programming.
// Core coroutine intrinsics
fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn(completion: Continuation<T>): Any?
fun <T> (suspend () -> T).createCoroutineUnintercepted(completion: Continuation<T>): Continuation<Unit>
fun <T> Continuation<T>.intercepted(): Continuation<T>
// Cancellation support
class CancellationException : IllegalStateExceptionRuntime reflection capabilities with JavaScript class interoperability for dynamic type inspection and instance creation.
// Core reflection interfaces
actual interface KClass<T : Any> {
val simpleName: String?
val qualifiedName: String?
}
actual interface KFunction<out R> : KCallable<R>
actual interface KProperty<out V> : KCallable<V>
// JavaScript integration
external interface JsClass<T : Any>
val <T : Any> KClass<T>.js: JsClass<T>
val <T : Any> JsClass<T>.kotlin: KClass<T>
fun <T : Any> KClass<T>.createInstance(): TDirect access to browser globals, DOM utilities, and local storage for web application development.
// Browser globals
external val window: Window
external val document: Document
external val localStorage: Storage
external val sessionStorage: Storage
// DOM utilities
fun Document.createElement(name: String, init: Element.() -> Unit): Element
fun Element.addClass(vararg cssClasses: String)
fun Element.removeClass(vararg cssClasses: String)
fun Element.hasClass(cssClass: String): Boolean
fun Node.clear()JavaScript-adapted mathematical functions and time/date utilities with proper type conversions and JavaScript Date integration.
// Time conversions
fun Instant.toJSDate(): Date
fun Date.toKotlinInstant(): Instant
// Duration units
actual enum class DurationUnit {
NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS
}
// Math functions (sample - full set available)
fun sin(x: Double): Double
fun cos(x: Double): Double
fun sqrt(x: Double): Double
fun pow(base: Double, exp: Double): DoubleBase64 encoding support and I/O operations optimized for JavaScript runtime environments.
// Base64 encoding
object Base64 {
fun encode(source: ByteArray): ByteArray
fun decode(source: ByteArray): ByteArray
fun encodeToString(source: ByteArray): String
fun decodeFromString(source: String): ByteArray
}⚠️ EXPERIMENTAL API: Secure UUID generation using Web Crypto API with platform-specific implementations for JavaScript environments.
// UUID functions (experimental, requires @OptIn(ExperimentalUuidApi::class))
@ExperimentalUuidApi
internal fun secureRandomUuid(): Uuid// Core JavaScript types
external interface dynamic
// JSON interface
external interface Json {
operator fun get(key: String): Any?
operator fun set(key: String, value: Any?)
}
// Promise type
external class Promise<out T> {
constructor(executor: (resolve: (T) -> Unit, reject: (Throwable) -> Unit) -> Unit)
}
// Regular expressions
external class RegExp {
constructor(pattern: String, flags: String? = definedExternally)
fun test(string: String): Boolean
fun exec(string: String): RegExpMatch?
}
external interface RegExpMatch {
val index: Int
val input: String
}