Skip to content

Instantly share code, notes, and snippets.

@JonDotsoy
Last active July 29, 2022 17:49
Show Gist options
  • Save JonDotsoy/1853aa951abdbaa8e6add78d12c8b668 to your computer and use it in GitHub Desktop.
Save JonDotsoy/1853aa951abdbaa8e6add78d12c8b668 to your computer and use it in GitHub Desktop.
Typescript Util Type Serializable

Serializable<Type>

Transform the type in a type serializable.

Sample:

type SerializableBase<T> = T extends string | number | boolean | null ? T :
T extends Function ? never :
T extends object ? { [K in keyof T]: ExcludeNever<SerializableBase<T[K]>> } :
never
type ExcludeNever<T> = T extends object ? Pick<T, { [K in keyof T]: T[K] extends never ? never : K }[keyof T]> :
T;
type Serializable<T> = ExcludeNever<SerializableBase<T>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment