Add UnwrapPartial type#1296
Conversation
|
Questions that popped up to my mind while working on this:
|
e97c402 to
de00d90
Compare
|
Aside from the points mentioned above, this PR is ready for review. Once these changes are in, I’ll be happy to follow up with another |
There was a problem hiding this comment.
Looks like the test cases could be simplified, there are so many test cases of the form UnwrapPartial<Partial<Something>>, with different values for Something. I think we don't need that many because the value of Something doesn't really matter, so we could just keep a few and remove the others.
Tests that don't have Partial are useful, like UnwrapPartial<{a?: string; b: number}>, UnwrapPartial<{a?: string; b?: number}>.
There was a problem hiding this comment.
Hey Som, thanks for the review.
I use UnwrapPartial<Partial<Something>> to ensure that the original Something type gets preserved. And there is a good number of types to consider.
Cases like UnwrapPartial<{a?: string; b: number}> and UnwrapPartial<{a?: string; b?: number}> are already covered (under `UnwrapPartial` preserves optional properties).
I’ll add any missing cases you pointed out, but feel free to prune things on your end as well.
There was a problem hiding this comment.
Cases like
UnwrapPartial<{a?: string; b: number}>andUnwrapPartial<{a?: string; b?: number}>are already covered (under`UnwrapPartial` preserves optional properties).
Yeah, I meant all of those are useful, don't remove them.
There was a problem hiding this comment.
@som-sm — Unless you want to make any other specific changes, I think this PR is ready to merge.
source/unwrap-partial.d.ts
Outdated
| : never | ||
| ) | ||
| : never; |
There was a problem hiding this comment.
Should these be never or PartialObjectType, what would be better?
There was a problem hiding this comment.
The way I see this, you shouldn’t unwrap a modifier on a type that doesn’t have one in the first place (goes for all Unwrap* types). UnwrapPartial<number> should never resolve to number.
I was also wondering earlier:
Should we be more strict about array types? Technically
UnwrapPartial<string[]>will pass asstring[], but it doesn’t make much sense (neither does definingPartial<string[]>).
After thinking it over, I’m leaning towards not giving arrays any special treatment. Let’s assume the user knows what they’re doing.
There was a problem hiding this comment.
After thinking it over, I’m leaning towards not giving arrays any special treatment. Let’s assume the user knows what they’re doing.
Yeah, that's fine.
The way I see this, you shouldn’t unwrap a modifier on a type that doesn’t have one in the first place (goes for all
Unwrap*types).UnwrapPartial<number>should never resolve tonumber.
I brought this up because with unions, IMO, it feels better if UnwrapPartial<Partial<{x: number, y: number}> | [number, number]>> returned {x: number, y: number} | [number, number] as opposed to just {x: number; y: number}. WDYT?
There was a problem hiding this comment.
A union with a partial isn’t technically a partial, and handling special cases is a can of worms we probably don’t want to open 🙂
There was a problem hiding this comment.
A union with a partial isn’t technically a partial
No, I did not mean that Partial<{x: number, y: number}> | [number, number] is a partial. I meant that instantiating UnwrapPartial with a union that contains both partial and non-partial items would cause the non-partial items to be removed, which might not be desirable.
handling special cases is a can of worms we probably don’t want to open
No, we'd obviously not handle unions separately, I meant replacing never with PartialObjectType. I think that'd be better.
There was a problem hiding this comment.
Writable (technically UnwrapReadonly) works that way already, so I’m fine with returning the original type for consistency. Just made the necessary edits.
![]()
There was a problem hiding this comment.
Writable(technicallyUnwrapReadonly)
They're not the same, right? Just like Required and UnwrapPartial:
type Config = Partial<{port: number; host: string; secure?: boolean}>;
//=> {port?: number; host?: string; secure?: boolean}
type T1 = UnwrapPartial<Config>;
//=> {port: number; host: string; secure?: boolean}
type T2 = Required<T1>;
//=> {port: number; host: string; secure: boolean}so I’m fine with returning the original type for consistency. Just made the necessary edits.
👍
6c84802 to
839f6b3
Compare
som-sm
left a comment
There was a problem hiding this comment.
PR LGTM! Apologies for the delay.
|
All good. Thanks for the passing review! @som-sm @sindresorhus — Are you open to having a conversation about adding more |
Yeah sure! And feel free to open PRs for other |
Closes #1294.