Gabriel Petersson
Gabriel Petersson

Reputation: 10412

Omit function properties from a typescript interface

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

Upvotes: 2

Views: 142

Answers (1)

Tobias S.
Tobias S.

Reputation: 23795

Here you go:

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

Playground

Upvotes: 2

Related Questions