-
-
Notifications
You must be signed in to change notification settings - Fork 679
Add UnwrapPartial type
#1296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sindresorhus
merged 3 commits into
sindresorhus:main
from
porada:feature/unwrap-partial
Dec 25, 2025
+118
−0
Merged
Add UnwrapPartial type
#1296
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| Revert the `Partial` modifier on an object type. | ||
|
|
||
| Use case: Infer the underlying type `T` when only `Partial<T>` is available or the original type may not be directly accessible. | ||
|
|
||
| @example | ||
| ``` | ||
| import type {UnwrapPartial} from 'type-fest'; | ||
|
|
||
| type Config = Partial<{ | ||
| port: number; | ||
| host: string; | ||
| secure?: boolean; | ||
| }>; | ||
|
|
||
| type InitializedConfig = UnwrapPartial<Config>; | ||
| //=> {port: number; host: string; secure?: boolean} | ||
| ``` | ||
|
|
||
| Note: If the provided type isn’t of `Partial<T>`, `UnwrapPartial` has no effect on the original type. | ||
|
|
||
| @category Object | ||
| */ | ||
| export type UnwrapPartial<PartialObjectType> = | ||
| PartialObjectType extends Partial<infer ObjectType> | ||
| ? ( | ||
| Partial<ObjectType> extends PartialObjectType | ||
| ? ObjectType | ||
| : PartialObjectType | ||
| ) | ||
| : PartialObjectType; | ||
|
|
||
| export {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import {expectType} from 'tsd'; | ||
| import type {EmptyObject, UnwrapPartial} from '../index.d.ts'; | ||
|
|
||
| type TestType = { | ||
| a: string; | ||
| b: number; | ||
| }; | ||
|
|
||
| expectType<TestType>({} as UnwrapPartial<Partial<TestType>>); | ||
| expectType<TestType>({} as UnwrapPartial<{a?: string; b?: number}>); | ||
| expectType<Partial<TestType>>({} as UnwrapPartial<Partial<Partial<TestType>>>); | ||
|
|
||
| // `UnwrapPartial` preserves optional properties | ||
| type TestTypeWithOptionalProp = TestType & { | ||
| c?: boolean; | ||
| }; | ||
|
|
||
| type AnotherTestType = { | ||
| c: boolean; | ||
| d: 'literal'; | ||
| }; | ||
|
|
||
| type TestTypeWithOptionalProps = TestType & Partial<AnotherTestType>; | ||
|
|
||
| expectType<TestTypeWithOptionalProp>({} as UnwrapPartial<Partial<TestTypeWithOptionalProp>>); | ||
| expectType<TestTypeWithOptionalProps>({} as UnwrapPartial<Partial<TestTypeWithOptionalProps>>); | ||
|
|
||
| // `UnwrapPartial` preserves nested `Partial` properties | ||
| type TestTypeWithPartialProp = TestType & { | ||
| c: Partial<TestType>; | ||
| }; | ||
|
|
||
| expectType<TestTypeWithPartialProp>({} as UnwrapPartial<Partial<TestTypeWithPartialProp>>); | ||
|
|
||
| // `UnwrapPartial` preserves readonly properties | ||
| type TestTypeWithReadonlyProps = Readonly<TestType> & { | ||
| readonly c: boolean; | ||
| }; | ||
|
|
||
| expectType<TestTypeWithReadonlyProps>({} as UnwrapPartial<Partial<TestTypeWithReadonlyProps>>); | ||
|
|
||
| // `UnwrapPartial` works with methods | ||
| type TestTypeWithMethod = { | ||
| c(): void; | ||
| }; | ||
|
|
||
| expectType<TestTypeWithMethod>({} as UnwrapPartial<Partial<TestTypeWithMethod>>); | ||
|
|
||
| // `UnwrapPartial` works with union types | ||
| type PartialUnionType = Partial<TestType> | Partial<AnotherTestType>; | ||
|
|
||
| expectType<TestType | AnotherTestType>({} as UnwrapPartial<PartialUnionType>); | ||
| expectType<TestType | AnotherTestType>({} as UnwrapPartial<Partial<TestType> | AnotherTestType>); | ||
|
|
||
| // `UnwrapPartial` works with index signatures | ||
| type ArrayLikeTestType = { | ||
| [index: number]: string; | ||
| }; | ||
|
|
||
porada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| type PlainObjectTestType = { | ||
| readonly [key: string]: number | undefined; | ||
| }; | ||
|
|
||
| expectType<ArrayLikeTestType>({} as UnwrapPartial<Partial<ArrayLikeTestType>>); | ||
| expectType<PlainObjectTestType>({} as UnwrapPartial<PlainObjectTestType>); | ||
| expectType<Record<number, string>>({} as UnwrapPartial<Partial<Record<number, string>>>); | ||
| expectType<string[]>({} as UnwrapPartial<Partial<string[]>>); | ||
porada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| expectType<Array<string | undefined>>({} as UnwrapPartial<Array<string | undefined>>); | ||
|
|
||
| // `UnwrapPartial` works with tuples | ||
| type TestTuple = [string, number, boolean]; | ||
|
|
||
| expectType<TestTuple>({} as UnwrapPartial<Partial<TestTuple>>); | ||
| expectType<TestTuple>({} as UnwrapPartial<[string?, number?, boolean?]>); | ||
|
|
||
| // `UnwrapPartial` works with empty objects and unknown types | ||
| expectType<EmptyObject>({} as UnwrapPartial<Partial<EmptyObject>>); | ||
| expectType<unknown>({} as UnwrapPartial<Partial<unknown>>); | ||
| expectType<any>({} as UnwrapPartial<Partial<any>>); | ||
| expectType<Record<string, unknown>>({} as UnwrapPartial<Partial<Record<string, unknown>>>); | ||
| expectType<Record<string, any>>({} as UnwrapPartial<Partial<Record<string, any>>>); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the test cases could be simplified, there are so many test cases of the form
UnwrapPartial<Partial<Something>>, with different values forSomething. I think we don't need that many because the value ofSomethingdoesn't really matter, so we could just keep a few and remove the others.Tests that don't have
Partialare useful, likeUnwrapPartial<{a?: string; b: number}>,UnwrapPartial<{a?: string; b?: number}>.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey Som, thanks for the review.
I use
UnwrapPartial<Partial<Something>>to ensure that the originalSomethingtype gets preserved. And there is a good number of types to consider.Cases like
UnwrapPartial<{a?: string; b: number}>andUnwrapPartial<{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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I meant all of those are useful, don't remove them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@som-sm — Unless you want to make any other specific changes, I think this PR is ready to merge.