Recently, I developed a code snippet in the playground aimed at producing an intersection type. This intersection type would allow for either utilizing new
to create a specific type, or invoking the constructor instead. Fortunately, this code is functional within the playground:
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 {
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();
}
Despite functioning smoothly within the playground, when attempted on VS 2017 with the latest updates, it fails completely. Specifically, the line $TestA extends MyObject
triggers an error stating
Type ICallableType<TInstance, TType> & TType is not a constructor function
. Do you have any insights into why this discrepancy exists? Could it be a result of an update causing a breaking change?