Javascript structure representation using Kotlin JS

my question is about creating javascript structure within KotlinJS and use them calling external modules. Let’s say we have the following js code and we want to translate it into KotlinJS.

const config = {
defs : "something",
resolvers : {
  Query: {
    books: () => []
  }}
};

myFunction(config) // This can be any kind of external js function that accepts the above structure

How do we represent that config structure above using Kotlin JS? Is there an easy way to handle structures/json Kotlin side? Can we declare in some way that structure as dynamic?

This answer comes from one of the Dukat authors @shabunc

As of now you gotta introduce interface and it’s implementation, so it would be something like this:

external interface ConfigInterface {
  var defs: String,
  var resolvers: QueryHolder
}

external interface QueryHolder {
   var Query: BookProcessor
}

external interface BookProcessor {
   var books: () -> Array<Any>
}

For more complicated structures it can easily become a challenge. Here’s what can be done to automate such translations. You can:

  • generate typescript declaration for this code with typescript compiler (using tsc -d )
  • generate kotlin declaration with dukat.

Dukat is a tool from Kotlin/JS team created specifically for this, there’s ongoing battle for improving the quality of this tool. Here is what would be generated in your particular case:

external interface `T$0` {
    var books: () -> Array<Any>
}

external interface `T$1` {
    var Query: `T$0`
}

external object config {
    var defs: String
    var resolvers: `T$1`
}

Which is far from optimal - for instance the name of generated entities is something we didn’t wanted to encourage people to reuse but the more it goes, the more it looks like a mistake (which we will fix this way or another).