Omit function properties from a typescript interface

Viewed 41

I want to Omit all functions from an interface

interface Human {
  name: string;
  age: number;
  walk: () => void;
  talk: (word: string) => Promise<void>
}

type HumanWithoutFunctions = RemoveFunctions<Human>

/* expected result:

HumanWithoutFunctions {
  name: string;
  age: number;
}
*/
1 Answers

Here you go:

type RemoveFunctions<T> = {
  [K in keyof T as T[K] extends Function ? never : K]: T[K]
}

Playground

Related