-
-
Notifications
You must be signed in to change notification settings - Fork 714
Add UnwrapRequired type
#1410
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 9 commits into
sindresorhus:main
from
porada:feature/unwrap-required
May 31, 2026
+114
−0
Merged
Add UnwrapRequired type
#1410
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e55306d
Introduce `UnwrapRequired` type
porada 4d2a5ad
Add documentation
porada 6ac5aa3
Refactor tests
porada 4be8ff9
Simplify type
porada c844694
Add note on required tuples
porada ca9c34c
Handle additional cases
porada 3548950
Refactor type
porada 667dbf5
refactor: simply exit conditional
som-sm bce51b2
doc: simplify note text
som-sm 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,37 @@ | ||
| import type {MapsSetsOrArrays, NonRecursiveType} from './internal/type.d.ts'; | ||
|
|
||
| /** | ||
| Revert the `Required` modifier on an object type. | ||
|
|
||
| Use-case: Infer the underlying type `T` when only `Required<T>` is available or the original type may not be directly accessible. | ||
|
|
||
| @example | ||
| ``` | ||
| import type {UnwrapRequired} from 'type-fest'; | ||
|
|
||
| type ContactFormData = Required<{ | ||
| email: string; | ||
| message?: string; | ||
| }>; | ||
|
|
||
| type DraftContactFormData = UnwrapRequired<ContactFormData>; | ||
| //=> {email: string; message?: string} | ||
| ``` | ||
|
|
||
| Note: | ||
| - If the provided type isn’t of the form `Required<T>`, `UnwrapRequired` simply returns the input type. | ||
| - `UnwrapRequired` doesn't work with arrays, if instantiated with arrays, it simply returns the input type. | ||
|
|
||
| @category Object | ||
| */ | ||
| export type UnwrapRequired<RequiredObjectType> = | ||
| RequiredObjectType extends NonRecursiveType | MapsSetsOrArrays | ||
| ? RequiredObjectType | ||
| : _UnwrapRequired<RequiredObjectType>; | ||
|
|
||
| type _UnwrapRequired<RequiredObjectType> = | ||
| RequiredObjectType extends Required<infer ObjectType> | ||
| ? ObjectType | ||
| : RequiredObjectType; | ||
|
|
||
| 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,73 @@ | ||
| import {expectType} from 'tsd'; | ||
| import type {UnwrapRequired} from '../index.d.ts'; | ||
|
|
||
| type TestType = { | ||
| a?: string; | ||
| b: number; | ||
| }; | ||
|
|
||
| expectType<TestType>({} as UnwrapRequired<Required<TestType>>); | ||
|
|
||
| // `UnwrapRequired` preserves optional properties | ||
| type AnotherTestType = { | ||
| c?: boolean; | ||
| d?: 'literal'; | ||
| }; | ||
|
|
||
| type TestTypeWithOptionalProps = TestType & AnotherTestType; | ||
|
|
||
| expectType<TestTypeWithOptionalProps>({} as UnwrapRequired<Required<TestTypeWithOptionalProps>>); | ||
|
|
||
| // `UnwrapRequired` preserves nested `Required` properties | ||
| type TestTypeWithRequiredProp = TestType & { | ||
| c: Required<AnotherTestType>; | ||
| }; | ||
|
|
||
| expectType<TestTypeWithRequiredProp>({} as UnwrapRequired<Required<TestTypeWithRequiredProp>>); | ||
|
|
||
| // `UnwrapRequired` preserves readonly properties | ||
| type TestTypeWithReadonlyProps = { | ||
| readonly a?: string; | ||
| readonly b: number; | ||
| readonly c?: boolean; | ||
| }; | ||
|
|
||
| expectType<TestTypeWithReadonlyProps>({} as UnwrapRequired<Required<TestTypeWithReadonlyProps>>); | ||
|
|
||
| // `UnwrapRequired` works with methods | ||
| type TestTypeWithMethod = { | ||
| a?: string; | ||
| c?(): void; | ||
| }; | ||
|
|
||
| expectType<TestTypeWithMethod>({} as UnwrapRequired<Required<TestTypeWithMethod>>); | ||
|
|
||
| // `UnwrapRequired` works with union types | ||
| type RequiredUnionType = Required<TestType> | Required<AnotherTestType>; | ||
|
|
||
| expectType<TestType | AnotherTestType>({} as UnwrapRequired<RequiredUnionType>); | ||
| expectType<TestType | AnotherTestType>({} as UnwrapRequired<Required<TestType> | AnotherTestType>); | ||
| expectType<TestType | string>({} as UnwrapRequired<Required<TestType> | string>); | ||
| expectType<TestType | Map<string, string>>({} as UnwrapRequired<Required<TestType> | Map<string, string>>); | ||
| expectType<TestType | Map<string, string> | string>({} as UnwrapRequired<Required<TestType> | Map<string, string> | string>); | ||
|
|
||
| // `UnwrapRequired` works with unknown types | ||
| expectType<unknown>({} as UnwrapRequired<Required<unknown>>); | ||
| expectType<any>({} as UnwrapRequired<Required<any>>); | ||
|
|
||
| // `UnwrapRequired` has no effect on non-required types | ||
| expectType<TestType>({} as UnwrapRequired<TestType>); | ||
| expectType<{a: string; b: number}>({} as UnwrapRequired<{a: string; b: number}>); | ||
| expectType<readonly string[]>({} as UnwrapRequired<readonly string[]>); | ||
| expectType<readonly [string, number?]>({} as UnwrapRequired<readonly [string, number?]>); | ||
| expectType<Set<string>>({} as UnwrapRequired<Set<string>>); | ||
| expectType<ReadonlySet<string>>({} as UnwrapRequired<ReadonlySet<string>>); | ||
| expectType<WeakSet<{a: string}>>({} as UnwrapRequired<WeakSet<{a: string}>>); | ||
| expectType<Map<string, string>>({} as UnwrapRequired<Map<string, string>>); | ||
| expectType<ReadonlyMap<string, string>>({} as UnwrapRequired<ReadonlyMap<string, string>>); | ||
| expectType<WeakMap<{a: string}, string>>({} as UnwrapRequired<WeakMap<{a: string}, string>>); | ||
| expectType<Date>({} as UnwrapRequired<Date>); | ||
| expectType<RegExp>({} as UnwrapRequired<RegExp>); | ||
| expectType<Promise<string>>({} as UnwrapRequired<Promise<string>>); | ||
| expectType<string>({} as UnwrapRequired<string>); | ||
| expectType<() => string>({} as UnwrapRequired<() => string>); |
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.
Uh oh!
There was an error while loading. Please reload this page.