TypeScript definitions und function patches for the Kotlin Standard Library
This project is maintained by mtm-solutions
npm install --save @mtm-solutions/kotlin-stdlib-wrapper
TypeScript definitions and function mapping for the Kotlin Standard Library.
This allows for the interoperation of TypeScript and Kotlin code. Kotlin modules can be embedded into a TypeScript web application and then typesafely called from there.
We applied this technique in our company to be able to use Kotlin business logic modules in an Angular frontend.
Kotlin supports function overloads, but TypeScript / JavaScript do not. When compiling down to JavaScript, the Kotlin compiler therefore appends a unique signature to the original function name that is generated from the type signature of the function. For example, Kotlin
List.get(index: Int): E
becomes
List.get_za3lpa$(index: number): E
in JavaScript / TypeScript. This library does not only provide TypeScript type definitions for the Kotlin standard library, but it also attaches delegator functions its class prototypes. The respective functions can be called from the TypeScript side in a simple way:
List.get(index: number): E
import {kotlin} from "@mtm-solutions/kotlin-stdlib-wrapper";
// Kotlin collections: create a list.
const elements = kotlin.collections.lisOf([1,2,3,4,5])
// Kotlin stream API: create list of squares.
const squares = elements.map(it => it*it)
// Kotlin stream API: prints 1, 4, 9, 16, 25.
squares.forEach(it => console.log(it))
// Native JS iterators: prints 1, 4, 9, 16, 25.
for (const it of squares) {
console.log(it)
}
//Convert a Kotlin list into a JS array
const squaresArray = squares.toArray()