Closed RyanCavanaugh closed 8 years ago
We'll close this with the assumption that #12114 (which is merged now) addresses all these use cases adequately - chime in with a new issue if that turns out not to be the case.
Hi @RyanCavanaugh, mapped types are fantastic, but it doesn't seem to address the questions above with deep partial types / deep subsets. Mapped types and Partial<>
allow for a shallow subset, but all nested objects must be complete (though there are some dirty solutions). A PartialDeep
generic would be fantastic.
The use case is that there is an API I'm building against which allows for object merges of very complex/deep objects.
@connor4312
type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
this works just fine for me.
@Igorbek
This gives zero control over Arrays for instance. It will iterate over their keys and allow you to access their method as nullable ones (concat, reduce, etc) which makes no sense. Same with a Date, etc.
ok, understood. My use case did require it. You then might be interested in #6606 with its mapping capabilities.
Also the related PR #17961. In fact, I'm facing the same problem. Will be extremely delighted when a solution becomes available.
This is a proposal for #4889 and a variety of other issues.
Use Cases
Many libraries, notably React, have a method which takes an object and updates corresponding fields on some other object on a per-key basis. It looks like this
Observations
query
); this is a "shallow" operationUnary
partial T
operatorA type
partial T
behaves as follows:T
, except that those properties are now optionalS
type is only assignable to typepartial T
ifS
has at least one property in common withT
partial (T | U)
is equivalent to(partial T) | (partial U)
partial (T & U)
does not expand (this would interact poorly with rule 2)T
is always a subtype ofpartial T
partial T
is equivalent toT
ifT
isany
,never
,null
, orundefined
More thoughts to come later, likely