question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generate exportable external interfaces for encodeToDynamic/decodeFromDynamic

See original GitHub issue

What is your use-case and why do you need this feature? We use kotlinx.serialization to map json strings from our API response into Kotlin data classes.

We also use kotlinx.serialization to

  • map our Kotlin data classes into standard JS objects using encodeToDynamic for JS consumers of our KMP library and
  • JS Objects back to data classes using decodeFromDynamic when JS consumers pass in data

This is great because it allows us to reuse our existing serialization logic for network requests, while exposing data in a shape that JS consumers expect.

It’s much faster than manually declaring external interfaces for each data class and writing mapping methods.

However, these methods only return/consume dynamic. This means that we don’t get Typescript definitions like we would with a standard external interface in the new JS IR Backend.

Ideally, we would be able to have external interfaces generated based on our SerialDescriptors, and be able to export them for use in our JS clients so they know what to expect.

Describe the solution you’d like

  • Serializer walks through the SerialDescriptors for each option
  • Serializer generates external interface representations of what it’s spitting out
    • acceptable if there are reasonable limitations on what can be converted to an external interface
  • allows users to mark external interface with @JsExport to get type definitions
  • encodeToDynamic can be cast to that generated external interface

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
aSemycommented, Apr 4, 2022

@ankushg Hi, I’ve written a prototype library that will generate TypeScript interfaces based on @Serializable classes.

https://github.com/adamko-dev/kotlinx-serialization-typescript-generator

@Serializable
class MyClass(
  val aString: String,
  var anInt: Int,
  val aDouble: Double,
  val bool: Boolean,
  private val privateMember: String,
)

fun main() {
  val tsGenerator = KxsTsGenerator()
  println(tsGenerator.generate(MyClass.serializer()))
}

Generated TypeScript interface:

export interface MyClass {
  aString: string;
  anInt: number;
  aDouble: number;
  bool: boolean;
  privateMember: string;
}

Is this useful for you? It’s not finished, but if you have any thoughts, please let me know.

1reaction
sandwwraithcommented, Apr 6, 2022

@aSemy Looks great!

I assume ticket can be closed now, as I said, this is a bit out of scope of the core library

Read more comments on GitHub >

github_iconTop Results From Across the Web

FAQs: External Transactions and External Interfaces
Where do I submit external transactions for a CTS-enabled interface? ... real-time export transactions may create or worsen a capacity deficient condition, ...
Read more >
How to export an imported interface from ambient module?
The solution I am not looking for is, moving the interface into module declaration. Because I have lot of interfaces across files and...
Read more >
Exporting Platforms to Vitis - 2022.2 English - Xilinx
To export the hardware prior to synthesis, use the following steps: In the Flow Navigator, under IP integrator, click Generate Block Design. Alternatively, ......
Read more >
Types of Interfacing - Altair
HyperMesh uses three types of external interfacing. These include: Importing CAD-generated geometry or finite element model information; Exporting CAD geometry ...
Read more >
External Interfaces - CAESAR II - Help
Main window ribbon: Interfaces tab External interfaces allow data transfer between CAESAR II and other software applications. In most cases, data transfer ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found