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

returning new Class Expression, returns constructor instead of instance #6619

Closed
michalstocki opened this issue Jan 26, 2016 · 1 comment
Closed
Labels
Duplicate An existing issue was already created Fixed A PR has been merged for this issue

Comments

@michalstocki
Copy link

The problem I found can be reproduced with the following code, on typescript version 1.7.5. The problem exists in typescript from the release when class expressions (#497) were introduced.

function builder() {
    return new class House {
        public open():void {
            console.info('house is open');
        }
    }
}
const house = builder();
house.open();

is compiled to

function builder() {
    return new (function () {
        function House() {
        }
        House.prototype.open = function () {
            console.info('house is open');
        };
        return House;
    })();
}
var house = builder();
house.open();

and looks good, but throws in the console:

Uncaught TypeError: house.open is not a function

Thats, because from builder() is returned constructor of House instead of an instance. The example above works, when we assign constructor to a variable and then, create instance with the new keyword.

@ahejlsberg
Copy link
Member

Duplicate of #4630. It was fixed in #6004 and your example works with current compiler in master.

@ahejlsberg ahejlsberg added Duplicate An existing issue was already created Fixed A PR has been merged for this issue labels Jan 26, 2016
@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
Duplicate An existing issue was already created Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

2 participants