Optional field in a record best practice

I have:

type xyz = {
  a: int,
  b: option<string>
}

I could also do

type xyz = {
  a: int,
  b?: string
}

Which approach is would you use where?
I my case xyz is my own code and not part of a binding.
It is also not a props records.

1 Like

Personally I only use the optional fields for JS interop, most prominently option objects where usually everything is optional.

I still prefer the strictness of explicitly defined options even if that means that an “empty” instantiation is full of None’s. It’s similar to writing down the cases of a variant explicitly so that you can not forget to handle it when you add a new variant case.

1 Like

I think it really boils down to

Is it ok if I forget to instantiate b ?

If it’s ok and if you don’t need to explicitly give a value to b most of the time, then you can make it an optional field, otherwise make it a mandatory field of type option.

2 Likes