Christian Dräger
10/15/2020, 9:13 PMadk
10/16/2020, 8:27 AMadk
10/16/2020, 8:44 AMexternal
declarations generated by kotlin need a bit of massaging to be useful.
I wrote a simple unit test in TypeScript, using jest, which depended on the client I had written. This is where the fun starts.
1. You cannot @JsExport
a class that is @Serializable
using kotlinx-serialization.
2. A non-`@JsExport`'ed class that implements a `@JsExport`'ed interface is unusable (from typescrtip) as its members are all prefixed with _
, while those of the interface are not.
3. The combination of 1 & 2 means that you must duplication any class that you want to be part of your interface - one copy to serialize across the wire and one to actually use in your typescript.
4. The TypeScript .d.ts files generated by the IR compiler put all your code in a namespace matching your Kotlin package name, which is clunky to import from TypeScript. What we ended up with is something like:
import my from "my-module";
import my.package.MyClass = MyClass;
adk
10/16/2020, 8:56 AMexternal
declaration) does not get properly converted back to TypeScript when you @JsExport
it. The .d.ts file references the kotlin declaration and not the JS/TS code. This means that any function that returns a Promise will return a kotlin.js.Promise
and not a javascript Promise
, which is pretty meaningless in JS/TS and breaks async/await. You can work around this by returning dynamic
and casting the returned object, but that starts to discard some of the benefit of TypeScript.
2. A kotlin function with an expression body will cause the IR compiler to fail. Convert it to a block body and you're ok.
3. A kotlin function with a trailing line comment will cause the IR compiler to fail.
4. JsExporting classes with nested classes will cause the IR compiler to fail.
5. JsExporting classes with companion objects will cause the IR compiler to fail.
6. JsExporting a data class will leave you with pointless hashCode()
, equals()
and componentN()
methods in your typescript defintions.adk
10/16/2020, 8:58 AMChristian Dräger
10/16/2020, 10:23 AMadk
10/16/2020, 10:37 AMadk
10/20/2020, 10:12 AM@JsExport
should use an Array instead. Other collection types, you will need to figure out the trade off that works best for you.