0
我正在嘗試使用具有僞類的JS庫的Typescript,以及提供繼承的extend()
函數。我卡在如何通過不同的構造函數,以新的「類」:Typescript定義泛型構造函數
type A = (param1: string, param2: number);
type B = (param3: number, param4: number);
interface BaseClass {
extend<Constructor>(): {
new(); // How would I associate this function with type Constructor?
}
}
var NewClass = BaseClass.extend<A>();
new NewClass('Hello world', 3); // type checked against type A
var SecondClass= BaseClass.extend<B>();
new SecondClass(4, 5); // type checked against type B
我已經試過這樣:
interface BaseClass {
extend<Constructor>(): {
new(): Constructor;
}
}
但是預計new NewClass()
到返回 A型
我已經試過這樣:
interface BaseClass {
extend<Constructor>(): {
new<Constructor>();
}
}
但是,這不會輸入檢查new NewClass()
。
將函數與類型關聯的正確語法是什麼?