英文:
Typescript use method signature from generics
问题
可以像这样做吗?这将非常有帮助。使用泛型的方法签名:
class Foo<Parent extends Foo> {
public bar<U extends Parameters<Parent.bar>>(x: U) {
}
}
英文:
Is it possible to do something like this? It would help quite a lot. Us the method signature from generics
class Foo<Parent extends Foo> {
public bar<U extends Parameters<Parent.bar>>(x: U) {
}
}
答案1
得分: 1
以下是您要翻译的代码部分:
class Foo<Parent extends { bar: (...a: any[]) => any }> {
public bar<U extends Parameters<Parent['bar']>>(x: U) {
}
}
您还可能对这个将参数展开到bar
函数的版本感兴趣:
class Foo<Parent extends { bar: (...a: any[]) => any }> {
public bar(...x: Parameters<Parent['bar']>) {
}
}
<details>
<summary>英文:</summary>
You can't if you want `Parent` to extend `Foo`, since that would cause a circular dependency in the type, but you can do it if you constrain `Parent` to something else with a `bar`:
```ts
class Foo<Parent extends { bar: (...a: any[]) => any}> {
public bar<U extends Parameters<Parent['bar']>>(x: U) {
}
}
You might also be interested in this version that spreads the arguments back to the bar
function:
class Foo<Parent extends { bar: (...a: any[]) => any}> {
public bar(...x: Parameters<Parent['bar']>) {
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论