Reputation: 22859
In newer TypeScript versions (I think 2.8 onwards?), I can easily obtain the return type of a function:
function f() { return "hi"; }
type MyType = ReturnType<typeof f>; //MyType is string
But I can't figure out to get the same info from a class method…
class MyClass {
foo() { return "hi"; }
}
How do I get the return type of (new MyClass()).foo()
?
Upvotes: 44
Views: 13032
Reputation: 51669
To get property or method type, you can use indexed access type operator:
type FooReturnType = ReturnType<MyClass['foo']>;
Upvotes: 108