Skip to content

Instantly share code, notes, and snippets.

@dancrumb
Created December 10, 2019 21:39
Show Gist options
  • Save dancrumb/8c6a7be234341164ace00420bce1d6f8 to your computer and use it in GitHub Desktop.
Save dancrumb/8c6a7be234341164ace00420bce1d6f8 to your computer and use it in GitHub Desktop.
export type JSONType<T> = T extends Date
? string
: T extends number
? number
: T extends boolean
? boolean
: T extends string
? string
: T extends Array<infer F>
? __JSONNonArrayType<F>[]
: T extends {}
? __JSONObjectType<WithoutMethods<T>>
: never;
type __JSONNonArrayType<T> = T extends number
? number
: T extends boolean
? boolean
: T extends string
? string
: T extends {}
? __JSONObjectType<WithoutMethods<T>>
: never;
type __JSONObjectType<T extends object> = {
[K in keyof T]: T[K] extends Optional<infer U>
? JSONType<U> | undefined
: JSONType<T[K]>;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment