I wanted to create a generic type that will hold a method that exists on a clas with parameters of this method, but when I am providing the class instance to the generic I get [never, never].
I would use it this type inside a class so I provide a simplified example of potential usage
Example usage inside a class
type AnyClass = { new (...arg0: any): any }
type SingeTask<V extends InstanceType<AnyClass>> = [
Extract<keyof V, (...arg0: any) => any>,
Parameters<Extract<keyof V, (...arg0: any) => any>>
]
type Task<V extends InstanceType<AnyClass>> = {
task: SingeTask<V>
resolve: (value?: unknown) => void
reject: (reason: any) => void
}
class Queue<T extends AnyClass> {
instances: InstanceType<T>[] = []
queue: Task<T>[] = []
object: T
constructor(object: T) {
this.object = object
}
addInstance( number:number) {
for (i; i < number; i++) {
this.instances.push(new this.object())
}
runTasksInQueue(
instance: InstanceType<T>,
{ task, reject, resolve }: Task<T>
) {
try {
const respone = Reflect.apply(instance, ...task)
resolve(respone)
} catch (error) {
reject(error)
} finally {
const task = this.queue.pop()
if (task) {
this.runTasksInQueue(instance, task)
} else {
this.instances.push(instance)
}
}
}
addTaskToQueue(task: SingeTask<T>) {
return new Promise((resolve, reject) => {
const instance = this.instances.pop()
if (instance) {
this.runTasksInQueue(instance, { task, resolve, reject })
} else {
this.queue.push({ task, resolve, reject })
}
})
}
}
Usage of the class Queue
class mockClass {
sth?: number
constructor(n?: number) {
this.sth = n
}
getNumber(n: number) {
return n
}
}
const instanceQueue = new queue(mockClass)
instanceQueue.addInstance(5)
instanceQueue.addTaskToQueue() // Here I get error [never,never] but I would expect to be able to pass [getNumber,(number e.g - 10)] and receive 10