0
我寫了一些代碼,在操場上創建交集類型以允許使用new
創建類型,或者調用構造函數。這在操場上正常工作:類型...不是構造函數類型
module Test {
export interface IType<TType> {
new (...args: any[]): TType;
}
export interface ICallableType<TInstance, TType extends IType<TInstance>> {
(): TInstance;
new(): TInstance;
}
class $MyObject extends Object { // ('Object' is here just as an example)
static abc = 1;
x = 1;
y = 2;
}
function registerClass<TInstance, TType extends IType<TInstance>>(_type: TType)
: ICallableType<TInstance, TType> & TType
{
return null;
}
export var MyObject = registerClass<$MyObject, typeof $MyObject>($MyObject);
var o = MyObject();
o.x = 1;
export class $TestA extends MyObject {
a = 3;
}
var TestA = registerClass<$TestA, typeof $TestA>($TestA);
var a = TestA();
export class $TestB extends TestA {
b = 4;
}
var TestB = registerClass<$TestB, typeof $TestB>($TestB);
var b = TestB();
}
(see here)
使用VS 2017最新更新,這並不在所有的工作。 $TestA extends MyObject
因Type ICallableType<TInstance, TType> & TType is not a constructor function
而失敗。任何想法爲什麼?看來操場有一些舊版本,所以這是一個突破性的改變或什麼?
VS 2017中的TypeScript版本是什麼? – Paleo
你爲什麼從Object中派生類? –
VS 2017有v2.1.5(預覽有2.2) –