Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable of type 'class' in Typescript

I'd like to do this:

createClass(c:class):SomeInstance {
   return new class() as SomeInstance;
}

However, it says 'type expected' where I specify the :class part.

like image 225
nizzle Avatar asked Sep 16 '25 06:09

nizzle


1 Answers

Fixed :

function createClass<SomeInstance>(c:{new():SomeInstance}):SomeInstance {
   return new c();
}

More

You are basically saying that createClass takes a constructor (something that when called with new gives an instance) hence c:{new():SomeInstance}. And then the return type of createClass is that SomeInstance

PS: probably call it createInstance 🌹

like image 110
basarat Avatar answered Sep 17 '25 21:09

basarat