无法在类型参数上访问打字稿私有或受保护成员“某物”

Clo*_* Fu 5 oop class this typescript typescript-typings

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)


  }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在类本身中引用受保护的类成员的类型?

DaG*_*ner 4

通过使用类名而不是:可以轻松解决这个问题this

class SomeClass {
  protected someMethod(): void {

  }

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

  }
}
Run Code Online (Sandbox Code Playgroud)

游乐场链接