Convert type with option<string> to Js.Nullable.t<string> via PPX

Hey everyone!

I am looking to convert a very large type that has ~100 attributes (all mostly strings), its a large type that holds lots of template / page configurations (colors, text, etc) Anyways, we are using this in a NextJs project as well and as some of you probably know, serverside props complains of “undefined” being used over “null”.

Awesome article on this from @ryyppy - ReScript records, NextJS, undefined and getStaticProps - DEV Community

I was wondering if there was a generator (ppx decorator) I could use to simply convert option to Js.Nullable.t

type template = {
  title: string,
  description: option<string>

Convert to:

type template = {
  title: string,
  description: Js.Nullable.t<string>

Thanks in advance!

1 Like

Will you need to both serialize to nulls when sending and deserialize back to undefined on the client?

This is not the answer you were looking for, but there are a couple of functions you may like to consider:

  • Js.Json.serializeExn
  • Js.Json.deserializeUnsafe

I believe these may not be recommended for use, but perhaps it’s fine for simple cases. Maybe some others can explain why.

For a project I’ve worked on I created two similar types (one with option types and one with null types) and functions to transform between the two types, but I didn’t have a large number of fields to deal with so it was not much work, but I’d be interested to hear other suggestions for solution for this.

Yea, so for encode/decode JSON we use the @decco ppx.

As for the two types, that is exactly what I did so far to “get it working” - But thought there had to be a better way! :crossed_fingers:t2: