Removing a field from an object (structural typing)


#1

I’m finding myself doing a lot of work with Js.t('a) right now as I write my GraphQL server that interacts with Postgres on the back-end. It’s neat-o that Js.Obj.assign({"foo": 1}, {"bar": 2}) will return a JS object that has the type {. "foo": int, "bar": int}. It can make unions of objects!

But sometimes I need to remove an attribute from a JS object. Is there any way in the OCaml object type system to remove a field from an object? Or, I guess, if we’re talking about row polymorphism here, a way to specify that a row has been removed from a row-parameterized type?

That’d make my day.


#2

A little follow-up. @jaredly did some experimentation and found that Js.Object.assign doesn’t actually do a union of fields, it basically just disables type checking on those fields all together. So beware!

(hence the type signature returning {..})


#3

Does that mean there is no structural typing? (Asking because of the conversations on the latest podcast)


#4

No! There’s certainly still structural typing, but it doesn’t appear that Js.Object.assign will take the union of two structures. Rather I think it just returns a structure that has “any shape”.