adding SemiPartialType and associated tests #654
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Since having a struct with a mix of required and optional properties is so common, I figured it would be nice (and more performant) to have a purpose-built type. I know you could achieve similar by doing
intersection(type(...), partial(...))
, but this approach feels more readable and easy to refactor.To address the elephant in the room: I am aware that
SemiPartial
is not a great name - so maybe renaming the type is in order :)It is worth noting that the method signature for
semiPartial
is 100% backwards compatible withtype
-- therefore, it would be an option to simply replacetype
with this code, and simply extend its capabilities.It has been requested before: #450
There is another old but still active PoC pull request: #266
And it has been asked on stackoverflow a number of times (some of many examples below):
https://stackoverflow.com/questions/48230773/how-to-create-a-partial-like-that-requires-a-single-property-to-be-set
https://stackoverflow.com/questions/61311182/a-partly-partial-with-io-ts
In addition to the new functionality, the behavior of
TypeOf
is exactly as you would define an interface. In other words the typechecker will treat the two following examples identically: