Last active
June 4, 2023 07:46
-
-
Save domosedov/cd4454993cd938122044fc4aee280a45 to your computer and use it in GitHub Desktop.
TS Utils
This file contains 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 WithAutocomplete<T, U = string> = T | (U & Record<never, never>) | |
type ArrayItemType<T> = T extends (infer U)[] ? U : never | |
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>; | |
type TupleToObject< | |
T extends readonly any[], | |
M extends Record<Exclude<keyof T, keyof any[]>, PropertyKey>, | |
> = { [K in Exclude<keyof T, keyof any[]> as M[K]]: T[K] } | |
export type NullifyOptionalFields<Obj extends object> = { | |
[Key in keyof Obj]: undefined extends Obj[Key] | |
? Obj[Key] extends infer U | undefined | |
? U extends object | |
? NullifyOptionalFields<U> | null | |
: U | null | undefined | |
: Obj[Key] | |
: Obj[Key] extends object | |
? NullifyOptionalFields<Obj[Key]> | |
: Obj[Key] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment