I am trying to create an instance of a class, but the compile-time error is saying:
Cannot create an instance of the abstract class 'QueueProcess'.
However, I am not creating an instance of it, I am creating an an instance of a class that extends QueueProcess. So, why am I getting this error?
export class Queue<T extends QueueProcess> {
private _queue: T[] = []
private async runFirstProcess() {
let process = new this._queue[0]
}
}
export abstract class QueueProcess {
}
The code once compiled works fine, it is just throwing that compile-time error.
So, first of all, the line T extends QueueProcess means that T is an instance of QueueProcess, which won't be newable. To access the constructor type, you need T extends typeof QueueProcess.
But that won't work anyway, since QueueProcess itself extends QueueProcess, and since it's abstract, typescript will complain about that. So instead, make T extend a newable function that returns a QueueProcess, eg:
export class Queue<T extends new () => QueueProcess> {
private _queue: T[] = []
private async runFirstProcess() {
let process = new this._queue[0]()
}
}
export abstract class QueueProcess {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With