funtypes icon indicating copy to clipboard operation
funtypes copied to clipboard

Optional Properties

Open ForbesLindesay opened this issue 5 years ago • 3 comments

Currently you can create an object with optional properties by using an Intersection of a Record and a Partial. This has a number of issues though:

  1. It's not very intuitive, and leads to a lot of very verbose code if you do this frequently
  2. Intersections are not currently considered to be records when handling disjoint unions
  3. It will not work well if we want to introduce a "sealed" object type

Mitigation:

You can use .Or(Undefined) to create almost the same effect. Maybe this is good enough?

Requested in: https://github.com/pelotom/runtypes/issues/136

ForbesLindesay avatar Sep 06 '20 15:09 ForbesLindesay

There is some existing discussion of approaches in https://github.com/pelotom/runtypes/pull/113

I think given how most programs treat undefined/optional properties, we could probably just add .Optional() as an alias for .Or(Undefined)

ForbesLindesay avatar Sep 11 '20 02:09 ForbesLindesay

Alternatively we could follow the approach in https://github.com/pelotom/runtypes/pull/131 of creating a special type to represent the "Maybe" fields.

ForbesLindesay avatar Sep 11 '20 02:09 ForbesLindesay

This was implemented a few days ago in https://github.com/pelotom/runtypes/pull/113, any chance of porting that update forward?

MicahZoltu avatar Mar 29 '21 07:03 MicahZoltu