Skip to content

Instantly share code, notes, and snippets.

@soal
Last active July 19, 2024 11:01
Show Gist options
  • Save soal/f269ff18862e04b44d3b4b1781a63506 to your computer and use it in GitHub Desktop.
Save soal/f269ff18862e04b44d3b4b1781a63506 to your computer and use it in GitHub Desktop.
Creates type recursively excluding functions from given type
type ExcludeFunctions<T> = {
// We need to map over the keys directly to preserve optionality. We filter with "as"
[K in keyof T as T[K] extends Function ? never : K]:
// Exclude undefined from the check to properly handle optional properties
Exclude<T[K], undefined> extends Array<infer E>
? Array<ExcludeFunctions<E>>
: Exclude<T[K], undefined> extends Record<string, any>
? ExcludeFunctions<T[K]>
: T[K];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment