gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:49:03 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com