Search code examples
javascriptkotlingoogle-visualizationkotlin-js

Kotlin JS doesn't add "new" keyword where needed


I am creating interactive infographics using Google Charts and Kotlin JS. This is snippet from Quick Start Page.

var data = new google.visualization.DataTable();
<..>
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));

Here "new" keyword is used. I tried to rewrite this code using Kotlin.

val data = google.visualization.DataTable();
<..>
val chart = google.visualization.PieChart(document.getElementById('chart_div'));

But an error occurred saying that "new" keyword is missing in lines above. So Kotlin to JS compiler haven't added the keyword where they should be. Here is compiled JavaScript code.

var data = google.visualization.DataTable();
<..>
var chart =  google.visualization.PieChart(document.getElementById('chart_div'));

Is there correct way to avoid the error without using js() function?

Thank you.


Solution

  • The reason why the new keyword was not added is because google variable hadn't been added with external keyword. With this one Kotlin generates correct code.

    external object google {
        object visualization {
            class DataTable
        }
    }