funtypes
funtypes copied to clipboard
Optional Properties
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:
- It's not very intuitive, and leads to a lot of very verbose code if you do this frequently
- Intersections are not currently considered to be records when handling disjoint unions
- 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
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)
Alternatively we could follow the approach in https://github.com/pelotom/runtypes/pull/131 of creating a special type to represent the "Maybe" fields.
This was implemented a few days ago in https://github.com/pelotom/runtypes/pull/113, any chance of porting that update forward?