ts-generator icon indicating copy to clipboard operation
ts-generator copied to clipboard

Compile from Kotlin to JS with type definitions

Open MartinEden opened this issue 8 years ago • 3 comments

First let me say this isn't a problem I'm expecting the maintainer of this repository to fix. I just thought they might be in a good position to comment on what's possible.

I have a Kotlin library I want to use on the JVM and in TypeScript. Using the official Kotlin2JS compiler, I can compile my code to JS. But then I don't get type definitions. And using this library I can generate type definitions, but not the actual implementation.

Is there a good way of combining the two? Do you know if JetBrains plan to provide type definition generation as part of their Kotlin2JS compiler?

MartinEden avatar Apr 19 '17 12:04 MartinEden

Hi.

I've not yet tried Kotlin2JS, so I don't know much about how it works, but as long as the generated JS API generated is consistent with the original Kotlin API of your program, it should pose no special inconvenience.

This project was created to handle data types, so it has some important limitations with regard to generating definitions for libraries or applications (as opposed to just exporting some data types):

First, definitions for methods are not created. This is far from impossible though. Just take a look at how definitions for properties are created and you'll find it should not be very different from that. https://github.com/ntrrgc/ts-generator/blob/89cb3f4b83f7d5b126094372b14d7a9ecfea2e27/src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt#L224

Second, applications and libraries need to split their classes in packages, which are often nested. These can be mirrored with TypeScript namespaces, but currently ts-generator does not support them: it just generates a single flat definition file.

These limitations can be overcome although in practice bridging languages is rarely seamless, as every little impedance mismatch between the two needs to be worked around.

I would actually prefer if Kotlin2JS generated the definition files itself as an additional compilation artifact instead of using this library for that. Since they already have a compiler, they have all the needed type information at build time. This library on the other hand has to recreate that same information using reflection once the application is running (even if normally this means only some modules are imported without doing much). As far as I know there are no plans to support TypeScript definition generation in Kotlin2JS though...

ntrrgc avatar Apr 19 '17 13:04 ntrrgc

Yep, this definitely feels like something that Kotlin2JS should be doing itself. Typescript is pretty ubiquitous these days :)

dupski avatar Aug 15 '18 07:08 dupski

When will we see this feature is unclear but it is filed in the issue tracker: https://youtrack.jetbrains.com/issue/KT-16604

NikolayMetchev avatar Jan 07 '19 14:01 NikolayMetchev