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
}
}