Typed Object.map or Object.assign


#1

Imagine function Object.map, something like Js.Dict.map, but remembers which properties object has.
Example:

let res = map(string_of_int, { "x": 1, "y": 2 });
/* res : {. "x" : string, "y" : string } */

Is it possible to encode something like that in a type system?

Another one: Object.assign, which merges properties of all arguments.

let res = assign({ "x" : 1 }, { "y" : 2 });
/* res : {. "x" : int, "y" : int } */

#2

Object.assign is here: https://bucklescript.github.io/bucklescript/api/Js.Obj.html#VALassign

There’s no map for Js.t objects; you can’t use the JS object bindings in both record mode and hash map mode at the same time. Though you can use the following last-resort hack to convert a Js.t object to a Js.Dict.t: https://bucklescript.github.io/docs/en/interop-cheatsheet.html#identity-external

So external myUnsafeConverter: {. "x": int, "y": int} => Js.Dict.t(Int) = "%Identity";, then use Js.Dict.map


#3

This is not exactly what I meant.
Js.Obj.assign returns you “any” type Js.t({.. }).
I was looking for some ways to encode it’s return type correctly.


#4

You can annotating the result. The compiler can’t help you here