Typescript Private or protected member 'something' 无法在类型参数上访问

标签 typescript class oop this 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)


  }
}

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

最佳答案

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

class SomeClass {
  protected someMethod(): void {

  }

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

  }
}

Playground Link

关于Typescript Private or protected member 'something' 无法在类型参数上访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60436496/

相关文章:

objective-c - 为什么 NSRect、NSPoint 等是结构体,而不是类?

R 中的类 : S3 vs S4

ios - 这个方法的定义有什么问题? ( objective-C )

c++ - 在创建派生类对象时将参数传递给基类构造函数

angular - 通过验证将表单分成多个组件

reactjs - 类型 'IntrinsicAttributes & IntrinsicClassAttributes<DatePicker> & Readonly<{ children?: ReactNode; }> ...' 上不存在属性

validation - 在 Angular2 的自定义验证器中注入(inject)服务

typescript - 如何在不声明键类型的情况下声明对象值类型?

c++ - 来自 "Designed Patterns Explained"的桥接模式示例

javascript - Jquery:扩展对象来修改方法