luky
luky

Reputation: 2370

Typescript use method signature from generics

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) {
    }
}

Upvotes: 0

Views: 48

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249476

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:

class Foo<Parent extends { bar: (...a: any[]) => any}> {
    public bar<U extends  Parameters<Parent['bar']>>(x: U) {
    }
}

Playground Link

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']>) {
    }
}

Playground Link

Upvotes: 1

Related Questions