If I have some javascript json data, can I foreign import
that data over a user-defined newtype, but with a subset of the actual data? For example:
in javascript:
exports.foo = {foo: "foo", bar: "bar"}
in purescript:
newtype Foo = Foo {foo :: String}
foreign import foo :: Foo
I know that we can use foreign import data ...
to treat the data as a reference, but I would like some of the type information to be user accessible, and this just seemed like the easiest way.
If the type is an anonymous JS object (rather than a "class" object/constructed from a prototype etc.) then you could forgo the foreign import entirely, and just declare a type
or newtype
for the record, since PS and JS records correspond 1:1.
As for erasing part of the structure in the type, that's certainly possible - PS won't care if the record has extra fields at runtime, it will only ever generate code involving the listed fields (aside from with record-update-syntax, as that shallow-copies the object before updating - but again, that will be fine, it'll preserve the extra keys).
The only potential problem here would be if a record of your PS type is constructed and then passed back out JS, as then it will be missing the extra fields. If the record will never go back out to JS, and/or the missing keys don't matter, then it's all good.