Skip to content

Instantly share code, notes, and snippets.

@domosedov
Last active June 4, 2023 07:46
Show Gist options
  • Save domosedov/cd4454993cd938122044fc4aee280a45 to your computer and use it in GitHub Desktop.
Save domosedov/cd4454993cd938122044fc4aee280a45 to your computer and use it in GitHub Desktop.
TS Utils
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