# What is the preferred way of implementing a typescript interface?

**URL:** <https://discuss.kotlinlang.org/t/what-is-the-preferred-way-of-implementing-a-typescript-interface/7473>\
**Category:** JavaScript\
**Created:** [April 19, 2018, 2:45am UTC](https://discuss.kotlinlang.org/t/what-is-the-preferred-way-of-implementing-a-typescript-interface/7473 "2018-04-19T02:45:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ansman](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ansman/32/4631_2.png) [@ansman](https://discuss.kotlinlang.org/u/ansman)\
**Post date:** [April 19, 2018, 2:45am UTC](https://discuss.kotlinlang.org/t/what-is-the-preferred-way-of-implementing-a-typescript-interface/7473/1 "2018-04-19T02:45:57Z")

</div>

I’m trying to interface with a typescript library that deals a lot with Json.

They define a lot of interfaces for declaring what the Json must contain which is nice for type safety but it’s causing some problems in Kotlin. When I try to implement the interface and then serialize that object as Json I get mangled names in the Json due to how Kotlin implements properties.

For example, given these declarations:

```kotlin
external interface Foo {
    val foo: String
}

class FooImpl : Foo {
    override val foo = "foo"
}

```

Kotlin will generate something like this:

```javascript
function FooImpl() {
  this.foo_new1zk$_0 = 'foo';
}
Object.defineProperty(FooImpl.prototype, 'foo', {
  get: function () {
    return this.foo_new1zk$_0;
  }
});
FooImpl.$metadata$ = {
  kind: Kind_CLASS,
  simpleName: 'FooImpl',
  interfaces: []
 };

```

which leads to this `JSON.stringify(Foo())` outputting `{"foo_new1zk$_0":"foo"}` rather than the desired `{"foo":"foo"}`.

One way to solve it is to use `json()` and `asDynamic()` but then I lose the type safety which isn’t what I want.

Are there any other solutions to this problem?

---

<div class="post-metadata">

**Author:** ![Konstantin-Khvan](https://avatars.discourse-cdn.com/v4/letter/k/4af34b/32.png) [@Konstantin-Khvan](https://discuss.kotlinlang.org/u/Konstantin-Khvan)\
**Post date:** [April 20, 2018, 7:55pm UTC](https://discuss.kotlinlang.org/t/what-is-the-preferred-way-of-implementing-a-typescript-interface/7473/2 "2018-04-20T19:55:09Z")

</div>

You don’t have to inherit external interfaces for json objects  
see JQueryAjaxSettings sample [https://kotlinlang.org/docs/reference/js-interop.html](https://kotlinlang.org/docs/reference/js-interop.html)
