How to get the type of a static property on a TypeScript class?

Viewed 1064

I have a helper class with some static methods:

export default class MyHelper {
    private constructor() {}
    private static privateMethod() {}
    public static publicHelperMethod() {}
}

I have a React component that uses publicHelperMethod and I want to ensure that this specific method is passed in my props declaration.

The way I have attempted to this is:

type LoggerMethod = MyHelper.publicHelperMethod;

But this throws an error and says MyHelper is being used as a namespace.

I can make the class itself a type:

type Helper = MyHelper;

But I'm only interested in my logger method (the public helper in this abstraction).

Is it possible to make a method of a class a type? If so, what is the syntax for this?

Or, am I going about this entirely the wrong way?

1 Answers

If you want to use the type of the method:

type LoggerMethod = typeof MyHelper.publicHelperMethod;

If you want to use the implementation of the method:

const loggerMethod = MyHelper.publicHelperMethod;
Related