0
我想能够传递一个类型(而不是类型的一个实例)作为参数,但我想执行一个规则,其中类型必须扩展一个特定的基类型TypeScript类型检查类型而不是实例
例
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 be allowed.
mangler.mangle(NotAShape); // should not be allowed.
从本质上讲,我想我需要更换shape: Function
的东西...别的吗?
这是可能的TypeScript?
注意:TypeScript也应该认识到shape
有一个默认构造函数。在C#中,我会做这样的事情...
class ShapeMangler
{
public void Mangle<T>() where T : new(), Shape
{
Shape shape = Activator.CreateInstance<T>();
// mangle the shape
}
}