Created
July 31, 2024 03:31
-
-
Save WomB0ComB0/1b1d60e16c19dc508cb4eea84b53c27f to your computer and use it in GitHub Desktop.
Custom Typescript Type Utils
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
type DeepReadonly<T extends Record<string, unknown>> = { | |
readonly [K in keyof T]: T[K] extends Record<string, any> | |
? T[K] extends (...args: Array<unknown>) => unknown | |
? T[K] | |
: DeepReadonly<T[K]> | |
: T[K]; | |
}; | |
type ReadonlyArray<T> = DeepReadonly<Array<T>>; | |
type DeepPartial<T extends Record<string, unknown>> = { | |
[K in keyof T]?: T[K] extends Record<string, any> | |
? T[K] extends (...args: Array<unknown>) => unknown | |
? T[K] | |
: DeepPartial<T[K]> | |
: T[K]; | |
}; | |
type PartialArray<T> = Array<T> | Array<PartialArray<T>>; | |
type DeepRequired<T extends Record<string, unknown>> = { | |
[K in keyof T]-?: T[K] extends Record<string, any> | |
? T[K] extends (...args: Array<unknown>) => unknown | |
? T[K] | |
: DeepRequired<T[K]> | |
: T[K]; | |
}; | |
type RequiredArray<T> = Array<T> | Array<RequiredArray<T>>; | |
type DeepNonNullable<T extends Record<string, unknown>> = { | |
[K in keyof T]: T[K] extends Record<string, any> | |
? T[K] extends (...args: Array<unknown>) => unknown | |
? T[K] | |
: DeepNonNullable<T[K]> | |
: NonNullable<T[K]>; | |
}; | |
type NonNullableArray<T> = Array<T> | Array<NonNullableArray<T>>; | |
type DeepNullable<T extends Record<string, unknown>> = { | |
[K in keyof T]: T[K] extends Record<string, any> | |
? T[K] extends (...args: Array<unknown>) => unknown | |
? T[K] | |
: DeepNullable<T[K]> | |
: T[K] | null; | |
}; | |
type NullableArray<T> = Array<T> | Array<NullableArray<T>>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment