Hi.
I’ve been trying to use ChartJs through KotlinJS.
First I used ts2kt (GitHub - Kotlin/ts2kt: ts2kt is officially deprecated, please use https://github.com/Kotlin/dukat instead. // Converter of TypeScript definition files to Kotlin external declarations) to get type bindings from Typescript (https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/chartjs) easily to Kotlin.
Excerpt of generated Kotlin kode:
external interface Chart {
fun Line(data: LinearChartData, options: LineChartOptions? = definedExternally /* null */): LinearInstance
fun Bar(data: LinearChartData, options: BarChartOptions? = definedExternally /* null */): LinearInstance
...
}
Looking at the test code in DefinetelyTyped chartjs, the chartjs is easily used from Typescript like this:
var myChart = new Chart(ctx).Bar(data, options)
However, the same is not as easy with Kotlin. I’ve been trying to something like this
val myChart = object : Chart(ctx) {
...
}
…but that does not work out good as
- the interface Chart (clearly) does not have a constructor
- it requires me to implement every method in my anonymous instance of Chart
Does anyone have better experience mapping existing js-libraries written like this? Is ts2kt leading me in the wrong direction? Should I change the generated kotlin code (maybe, from interface to class)? or is there a easier way to overcome this with using the generated kotlin code as is?