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

Support for Mix-in classes: can not export mixing function from a module #15313

Closed
ghost opened this issue Apr 22, 2017 · 5 comments
Closed

Support for Mix-in classes: can not export mixing function from a module #15313

ghost opened this issue Apr 22, 2017 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@ghost
Copy link

ghost commented Apr 22, 2017

TypeScript Version: 2.2

Code

// A *self-contained* demonstration of the problem follows...
export interface ITagged {
    _tag: string;
}

export type TAnyConstructor<T> = new (...args: any[]) => T;

export function MixedTagged<T extends TAnyConstructor<{}>>(Base: T) {
    return class extends Base implements ITagged {
        _tag: string;
        constructor(...args: any[]) {
            super(...args);
            this._tag = "";
        }
    }
}

Expected behavior:
Should be no error. The function returns a class which implements interface. It should be treated by compiler as original Base class which supports ITagged interface. Otherwise this mixing is useless.

Actual behavior:
Error Build:Return type of exported function has or is using private name '(Anonymous class)'

@ahejlsberg
Copy link
Member

Duplicate of #15066.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Apr 23, 2017
@mhegazy
Copy link
Contributor

mhegazy commented May 8, 2017

This issue seems to have been already addressed or is unactionable at the moment. I am auto-closing it for now.

@mhegazy mhegazy closed this as completed May 8, 2017
@ghost
Copy link
Author

ghost commented Nov 19, 2017

Thanks very much. I used it with typescript version 2.6.1. It works like a charm. Even allows to extend later the mixed classes.
That's how i used it in my project: see the MixedBaseObject function there
https://github.com/BBGONE/JRIApp/blob/master2/FRAMEWORK/CLIENT/shared/jriapp_shared/utils/mixobj.ts

@ghost
Copy link
Author

ghost commented Nov 23, 2017

Yes, it works, but the problem that i need to have a constructor. This mixing works only on classes. EcmaScript6 now has a Proxy object, which can be used to add properties, methods to a given object, thus adding members to the interface. I can not use this mixing function in this case. Types union does not work either, because T | U type is not the same as T extends U.

@ghost
Copy link
Author

ghost commented Dec 21, 2017

I've found that typescript has intersection types Type1 & Type2. I did not noticed it before. It is perfect for mixing.

@microsoft microsoft locked and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants