Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Private or protected member 'something' cannot be accessed on a type parameter

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?

like image 221
Cloud Soh Jun Fu Avatar asked Oct 16 '22 05:10

Cloud Soh Jun Fu


1 Answers

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

class SomeClass {
  protected someMethod(): void {

  }

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

  }
}

Playground Link

like image 50
ChrisG Avatar answered Oct 27 '22 01:10

ChrisG