I have this code:
const BlockConstructors: Function[] = [
OBlock,
IBlock,
TBlock
];
function randomFromArray(array: any[]) {
return array[Math.floor( Math.random() * array.length )];
}
const BlockConstructor: Function = random(BlockConstructors);
const block: Block = new BlockConstructor();
I try to draw a some block constructor from array and then create a new object, all my block constructors in array extends Block class. I get error:
Cannot use 'new' with an expression whose type lacks a call or construct signature.
Why this error appears?
Your code isn't self-contained, but here's the boiled down reason.
Function
isn't new
-able. Only three things can be new
'd in TypeScript:
void
any
You really want the first one.
Try switching from Function
to (new () => Block)
.
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