one thing that keeps bugging me is that @JsPlainOb...
# javascript
b
one thing that keeps bugging me is that @JsPlainObject does not implement ReadonlyRecordString, Any? so it's impossible to pass to APIs that work on records I've deliberately typed some APIs with Any instead of Record
t
It's possible, but use case isn't transparent :(
Why do you need unsafe API, when you have safe? :)
b
some JS APIs take generic objects
for the ones that I'm typing
talking about template APIs specifically
so passing an object to a template engine is better typed using @JsPlainObject, but then I can't pass it into that function
because that function takes a record, so I can't pass in an interface if that makes sense
In my specific use case, looking at Handlebars:
Copy code
var template = Handlebars.compile("Handlebars <b>{{doesWhat}}</b>");
template({ doesWhat: "rocks!" })
You might type the template function as
Copy code
external fun template(context: ReadOnlyRecord<String, Any?>): String
But since you know which variables are used inside the template, you might want to define the parameter as
Copy code
@JsPlainObject
external interface Context {
    val doesWhat: String
}
template(Context(doesWhat="rocks"))  // compile error
I think the solution is to generate a method/extension function called .toRecord()
Copy code
template(Context(doesWhat="rocks").toRecord())
e
In the meantime you can create a
Record
yourself I suppose.
Copy code
inline fun <K : Any, V> Any.toRecord(): Record<K, V> = unsafeCast<Record<K, V>>()
You could add whatever check to validate
this
b
right, gonna go with that approach for now
or rather: write one extension function for every interface I need to
e
But I think the idea to have a plain object implement Record<K, V> is valid. The meaning of safe interoping with JavaScript doesn't hold much value.
b
I think the main issue here is that js-plain-objects is in Kotlin core whereas record is in a community repo
are there any plans to merge this stuff together at some point?
e
Not that I know of. We currently have a very fragmented environment, indeed.
t
I think the main issue here is that js-plain-objects is in Kotlin core whereas record is in a community repo
It's not a blocker with K2
But I think the idea to have a plain object implement Record<K, V> is valid.
ReadonlyRecord
- fine
Record
- isn't fine
b
oh, that's great to hear 🙂