Type Alias JustMethods<T>

JustMethods<T>: Exclude<Pick<T, MethodNames<T>>, never>

Extract the methods from a class, return a new structure with methods only.

Useful for extending & mocking methods only

Type Parameters

  • T
class MyClass {
constructor() {}
aMethod() {}
anotherMethod() {}
aField = 'foo'
anotherField = 42
}

type MethodsMyClass = JustMethods<MyClass>
expectType<TypeEqual<MethodsMyClass, { aMethod: (arg: string) => string, anotherMethod: (num: number) => number }>>(true)

constructor is not included

Original code from https://zirkelc.dev/posts/extract-class-methods

Note: Exclude<..., never> is used to force TypeScript to report final type as an object, not a type helper call