TypeScript Version: nightly
Code
class A {}
const a: (typeof A) & {} = A;
class B extends a {}
Expected behavior:
No error.
Actual behavior:
src/a.ts(3,17): error TS2507: Type 'typeof A & {}' is not a constructor function type
Is there a real world scenario that needs this?
I was playing around with mixins, which involve intersecting class types. It would take more than just this to make those work, though!
The following might (or might not) be possible:
interface MixinStatics {
mixinStatic(): void;
}
interface MixinInstance {
mixinInstance(): void;
}
function mixin<Instance, Cls extends { new(): Instance }>(cls: Cls): { new(): Instance & MixinInstance } & MixinStatics & Cls {
return <any> class extends cls {
static mixinStatic() {}
mixinInstance() {}
}
}
class Super {}
// Type annotation hopefully not necessary
class C extends mixin<Super, typeof Super>(Super) { }
C.mixinStatic();
new C().mixinInstance();
One real world scenario is mixin http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/. If counting intersection type with construct signature would at least partially solve the long awaited mixin support in TypeScript.
Just got here with exactly same issue. Mixins are very important, so far possible with ES6, so would be great if we could actually do that in TS.
Seems quite handy in ES6
const Storage = Sup => class extends Sup {
save(database) { }
};
const Validation = Sup => class extends Sup {
validate(schema) { }
};
class Person { }
class Employee extends Storage(Validation(Person)) { }
Duplicate of #4890?
Most helpful comment
Seems quite handy in ES6
http://exploringjs.com/es6/ch_classes.html#_simple-mixins