Hey guys, how can one implement/call/create/whatev...
# javascript
j
Hey guys, how can one implement/call/create/whatever a kotlin interface from JS?
I have an interface in K/JS and I would like to pass its implementation to a method, which also is defined at the kotlin side, all from JS
s
K/JS interfaces do not correspond to anything idiomatic in JS. If absolutely necessary, you can see what K/JS compiler generates for interface implementation and try to mimic that. For example:
Copy code
// Kotlin
interface I
class C : I
// Generated JS
  function I() {
  }
  I.$metadata$ = {
    kind: Kind_INTERFACE,
    simpleName: 'I',
    interfaces: []
  };
  function C() {
  }
  C.$metadata$ = {
    kind: Kind_CLASS,
    simpleName: 'C',
    interfaces: [I]
  };
If you own K/JS code with given interface you can: a. make abstract class in K/JS and extend it like a regular JS class b. make interface
external
and create JS objects with matching structure
j
What's the difference between external and normal interfaces for JS?
s
And guarantees for as-is member names in JS
👍 1
j
That's nice, thank you!
u
@Svyatoslav Kuzmich [JB] @Jan Stoltman
And guarantees for as-is member names in JS
Not guarantees) https://kotlinlang.slack.com/archives/C0B8L3U69/p1559335458013500 https://kotlinlang.slack.com/archives/C0B8L3U69/p1559335973015500
s
@U75957 We do hear that people need more control over whether property: - is on prototype or on object itself - is enumerable - has value or accessor descriptor But name guarantees for
@JsName
or
external
properties are still there. What looks like a mangled property is likely a property's backing field which is not supposed to be accessed directly.