Creating Optional Fields out of strict record

How can I convert a record with strict fields to optional fields?
For example, if i have a record like

type t = { name : string, age : int}

i want some wrapper type like

type optional_t = MakeOptional(t)
-- same as:
-- type optional_t = { name ?: string, age ?: int} 

I don’t know, if there is a native way. Tried it several times. But there is a ppx. Have a look: GitHub - green-labs/ppx_ts: ReScript PPX helps the binding to typescript modules

If you want something like typescript Partial<T> utility type, then you’ll be disappointed by lack of support on Rescript

@saulpalv exactly I want something like Partial
not possible :slightly_smiling_face:?
and are there any good resources/tips for learning about the type-level stuff in rescript?

@dkirchhof thanks will give it a try

There’s no such thing as partial or type modifiers in rescript, the type system is relatively simple in rescript, which is usually a good thing!

1 Like

a bit verbose but I have used a pattern of making a structure type and then instantiating:

type structure<'a, 'b> = { name: 'a, age: 'b}
type t = structure<string, int>
type opt = structure<option<string>, option<int>>

The type system in ReScript requires you to think about things up front since you can’t modify the types later.

If you have a type with an optional field of name and a type where name is required, they aren’t really the same type.

You can use type spreading to share common keys across types.

type person = {
  name: string,
  age: int,
}
  

type user = {
  ...person,
  address?: string // we might not have an address for all users
}

type billing = {
  ...person,
  address: string // we have to have an address for shipping
}