Home > database >  Omit function properties from a typescript interface
Omit function properties from a typescript interface

Time:05-11

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;
}
*/

CodePudding user response:

Here you go:

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

Playground

  • Related