Typescript: Intersection type containing a constructor function type should count as one

Created on 10 Aug 2016  路  6Comments  路  Source: microsoft/TypeScript

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
Bug Fixed

Most helpful comment

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)) {  }

http://exploringjs.com/es6/ch_classes.html#_simple-mixins

All 6 comments

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)) {  }

http://exploringjs.com/es6/ch_classes.html#_simple-mixins

Duplicate of #4890?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kimamula picture kimamula  路  147Comments

rwyborn picture rwyborn  路  210Comments

OliverJAsh picture OliverJAsh  路  242Comments

nitzantomer picture nitzantomer  路  135Comments

disshishkov picture disshishkov  路  224Comments