0
似乎流中的類類型始終引用該類的實例,並且使用typeof
來引用實際的類本身。所以,如果我想要一個變量來引用基類的子類(不是實例),我可以這樣做:
class MyBaseClass {}
class MySubClass extends MyBaseClass {}
let a: $Subtype<MyBaseClass> = MySubClass; // fails
let b: $Subtype<MyBaseClass> = new MySubClass(); // works, but I don't want this.
let c: $Subtype<typeof MyBaseClass> = MySubClass; // works! Ok, we're good
不過,我似乎無法與類型參數做到這一點!例如,以下內容:
type GenericSubclass<T> = $Subtype<typeof T>;
// fails with `^ identifier `T`. Could not resolve name`
如果我嘗試以下打字稿招(見Generic and typeof T in the parameters),它也將失敗:
type ValidSubclass<T> = { new(): T };
const c: ValidSubclass<BaseClass> = MySubClass;
// fails with: property `new`. Property not found in statics of MySubClass
請注意,我試圖new
,__proto__
和constructor
。
什麼給?有沒有解決方法?