Is there a way to pass a type (not an instance) as a parameter, with the condition that the type must be an extension of a specific base type?
For example
abstract class Shape {
}
class Circle extends Shape {
}
class Rectangle extends Shape {
}
class NotAShape {
}
class ShapeMangler {
public mangle(shape: Function): void {
var _shape = new shape();
// mangle the shape
}
}
var mangler = new ShapeMangler();
mangler.mangle(Circle); // should work.
mangler.mangle(NotAShape); // should not work.
I think I might need to substitute shape: Function
with something else. Is this achievable in TypeScript?
Note: It's important for TypeScript to acknowledge that shape
has a default constructor. In C#, the equivalent would be...
class ShapeMangler
{
public void Mangle<T>() where T : new(), Shape
{
Shape shape = Activator.CreateInstance<T>();
// mangle the shape
}
}