Scenario
In my project, I have a unique class setup where methods are passed in as a list and can be called through the class with added functionality. These methods are bound to the class (Foo) when called, creating a specific type FooMethod
.
class Foo {
public constructor(methods) {}
}
const myMethod: FooMethod<number> = function(value: number): number {
return value
}
const myMethods = [myMethod]
const foo = new Foo(myMethods)
type FooMethod<T> = (this: typeof foo, ...args: any[]) => T
type MyMethodParameters = Parameters<typeof myMethod> // any[]
Challenge
The issue arises when assigning the FooMethod<number>
type to the myMethod
constant. This prevents accurately inferring the argument types using
Parameters<typeof myMethod>
.
Query
Is there a solution to ensure that the FooMethod
type retains the ability to infer arguments? Ideally,
Parameters<typeof myMethod>
would still provide the correct result of [number]
.