Skip to content

Commit

Permalink
feat(types): add DeepRequired
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed May 24, 2021
1 parent 8a3b158 commit 569a954
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ type DeepOmitHelper<T> =
| WeakMap<any, DeepOmitHelper<T>>

/**
* Recursive version of [`Partial`][1]
* Recursive version of [`Partial`][1].
*
* [1]: https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype
*/
Expand All @@ -142,10 +142,38 @@ export type DeepPartial<T> = T extends BuiltIn
: Array<DeepPartial<U>>
: T extends Promise<infer U>
? Promise<DeepPartial<U>>
: T extends UnknownObject
: T extends {}
? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>

/**
* Recursive version of [`Required`][1].
*
* See: [type-fest - Basic][2]
*
* [1]: https://www.typescriptlang.org/docs/handbook/utility-types.html#requiredtype
* [2]: https://github.com/sindresorhus/type-fest/blob/main/source/basic.d.ts
*/
export type DeepRequired<T> = T extends BuiltIn
? NonNullable<T>
: T extends Map<infer K, infer V>
? Map<DeepRequired<K>, DeepRequired<V>>
: T extends ReadonlyMap<infer K, infer V>
? ReadonlyMap<DeepRequired<K>, DeepRequired<V>>
: T extends WeakMap<infer K, infer V>
? WeakMap<DeepRequired<K>, DeepRequired<V>>
: T extends Set<infer U>
? Set<DeepRequired<U>>
: T extends ReadonlySet<infer U>
? ReadonlySet<DeepRequired<U>>
: T extends WeakSet<infer U>
? WeakSet<DeepRequired<U>>
: T extends Promise<infer U>
? Promise<DeepRequired<U>>
: T extends {}
? { [K in keyof T]-?: DeepRequired<T[K]> }
: NonNullable<T>

/**
* Type representing any empty object.
*/
Expand Down

0 comments on commit 569a954

Please sign in to comment.