Skip to content

class with static method and decorator outputs broken JavaScript #9957

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

Closed
d13 opened this issue Jul 26, 2016 · 2 comments
Closed

class with static method and decorator outputs broken JavaScript #9957

d13 opened this issue Jul 26, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@d13
Copy link

d13 commented Jul 26, 2016

TypeScript Version: 2.0.0

Code

function readonly<TFunction extends Function>(Target: TFunction): TFunction {
    let newConstructor = function () {
        Target.apply(this);
        Object.freeze(this);
    };

    newConstructor.prototype = Object.create(Target.prototype);
    newConstructor.prototype.constructor = Target;

    return <any> newConstructor;
}

@readonly
export class Foo {
    private bar() {
        return Foo.doBar();
    }
    static doBar() {
        return 'bar';
    }
}

Compile command-line:

tsc --target es6 --module system --experimentalDecorators  ./app/elements/foo.ts

note: this only occurs when using --module system

Expected behavior:
JS shouldn't throw for undeclared variable Foo_1.

Actual behavior:

System.register([], function(exports_1, context_1) {
    "use strict";
    var __moduleName = context_1 && context_1.id;
    var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    var Foo;
    function readonly(Target) {
        let newConstructor = function () {
            Target.apply(this);
            Object.freeze(this);
        };
        newConstructor.prototype = Object.create(Target.prototype);
        newConstructor.prototype.constructor = Target;
        return newConstructor;
    }
    return {
        setters:[],
        execute: function() {
            Foo_1 = class Foo {
                constructor() {
                    this.baz = 'baz';
                }
                bar() {
                    return Foo_1.doBar();
                }
                static doBar() {
                    return 'bar';
                }
            };
            let Foo = Foo_1;
            Foo = Foo_1 = __decorate([
                readonly
            ], Foo);
            exports_1("Foo", Foo);
        }
    }
});
@d13
Copy link
Author

d13 commented Jul 26, 2016

After installing typescript@next the code works.

@yuit
Copy link
Contributor

yuit commented Jul 27, 2016

@d13 this is a duplicate of #9685 which as you already notice the issue has been fixed in master. The bug will also be in final version of TypeScript 2.0.0. In the meantime, typescript@next will have the most latest fix of bugs discovered in 2.0.0

@yuit yuit closed this as completed Jul 27, 2016
@yuit yuit added the Duplicate An existing issue was already created label Jul 27, 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
Projects
None yet
Development

No branches or pull requests

2 participants