Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intersection type containing a constructor function type should count as one #10261

Closed
ghost opened this issue Aug 10, 2016 · 6 comments · Fixed by #13743
Closed

Intersection type containing a constructor function type should count as one #10261

ghost opened this issue Aug 10, 2016 · 6 comments · Fixed by #13743
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@ghost
Copy link

ghost commented Aug 10, 2016

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
@ahejlsberg
Copy link
Member

Is there a real world scenario that needs this?

@ghost
Copy link
Author

ghost commented Aug 12, 2016

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();

@HerringtonDarkholme
Copy link
Contributor

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.

@Buslowicz
Copy link

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.

@imcotton
Copy link
Contributor

imcotton commented Jan 2, 2017

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

@mhegazy mhegazy added the Bug A bug in TypeScript label Jan 4, 2017
@ghost
Copy link
Author

ghost commented Jan 28, 2017

Duplicate of #4890?

@ahejlsberg ahejlsberg assigned ahejlsberg and unassigned sandersn Jan 30, 2017
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Jan 30, 2017
@mhegazy mhegazy added this to the TypeScript 2.2 milestone Jan 30, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants
@imcotton @sandersn @HerringtonDarkholme @ahejlsberg @Buslowicz @mhegazy and others