is there a good, typesafe way to deal with APIs th...
# javascript
b
is there a good, typesafe way to deal with APIs that expect a TS Partial<SomeInterface>?
use case here is some kind of ORM that defines a bunch of fields on a model and has an update method which allows you to pass in a partial of said object
I could use DSLs for that but I'd like to avoid creating a DSL for each type, especially because keeping it in sync is difficult
another thing I tried was using a Proxy that kept track of setters and created the final record at the end; issue with that was that assigning a property to itself assigned a proxy to the proxy
it feels like this could maybe be solved by using a compiler plugin that generates a DSL based off a @JsPlainObject?
maybe something akin to
Copy code
@JsPlainObject
external interface NestedRecord {
    var name: String
}

@JsPlainObject
external interface ExampleRecord {
    var name: String
    var nested: NestedRecord
}

fun main() {
    val result: Record<JsAny, JsAny?> = partialOf<ExampleRecord> {
        name = "hi"
        nested = NestedRecord(name = "abc")
        nested {
            name = "ho"
        }
    }
}
I don't think you can just slap invoke onto a Record easily?
hm, works with an extension function
I guess this would still require a compiler plugin?
t
1. When you use
@JsPlainObject
it's highly recommended to use
val
s. Otherwise you will have broken nesting 😞
2.
Partial
potentially can be supported in Kotlin Wrappers. With common builder and adapter for transforming
X
->
Partial<X>