ForbesLindesay / funtypes

Runtime validation for static types
MIT License
29 stars 4 forks source link

Optional Properties #11

Open ForbesLindesay opened 4 years ago

ForbesLindesay commented 4 years ago

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 commented 3 years ago

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 commented 3 years ago

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

MicahZoltu commented 3 years ago

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