8
class SomeClass<T extends string> {
  protected someMethod(): void {

  }

  protected someOtherMethod(): ReturnType<this["someMethod"]> { 
  // Private or protected member 'someMethod' cannot be accessed on a type parameter.ts(4105)


  }
}

Is there any way to reference back the type of protected class member in the class itself?

1 Answer 1

5

This can easily solved by using the class name instead of this:

class SomeClass {
  protected someMethod(): void {

  }

  protected someOtherMethod(): ReturnType<SomeClass["someMethod"]> { 

  }
}

Playground Link

3
  • 1
    Here's a thing.. The class has some generic params, if I use your way, then I have to insert the generic again ReturnType<SomeClass<A,B>["someMethod"]>, I'm trying to save that effort Commented Feb 28, 2020 at 1:29
  • 1
    And the class name keep floating around will make it more complicated.. Commented Feb 28, 2020 at 1:34
  • Also, this refers to current class so it allows you to the type from a child class in case it overrides the method, so using direct class name will not allow you make that type dynamic for the children.
    – n1kk
    Commented Apr 18, 2024 at 11:15

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.