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

Decorators not working for class properties with union type #15046

Closed
vuorinem opened this issue Apr 6, 2017 · 2 comments
Closed

Decorators not working for class properties with union type #15046

vuorinem opened this issue Apr 6, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@vuorinem
Copy link

vuorinem commented Apr 6, 2017

If one of the types in union type is an imported class (say TestClass), it does not get defined for the module. When a decorator is added for the property, TS calls __metadata("design:type", test_class_1.TestClass), but test_class_1 is undefined, causing runtime error.

If the class is used elsewhere within the module, it will get properly defined. So an easy workaround is to call new TestClass() somewhere.

TypeScript Version: 2.2.2

Code

import { TestClass } from "./test-class";

function myDecorator(target, key) {
    console.log("Decorate...");
}

export class Things {
    @myDecorator
    private thing: TestClass | null;
}

Expected behavior:

define(["require", "exports", "./test-class"], function (require, exports, test_class_1) {
    // ...
    __decorate([
        myDecorator,
        __metadata("design:type", test_class_1.TestClass)
    ], Things.prototype, "thing", void 0);
    exports.Things = Things;
});

Actual behavior:

define(["require", "exports"], function (require, exports) {
    // ...
    __decorate([
        myDecorator,
        __metadata("design:type", test_class_1.TestClass)
    ], Things.prototype, "thing", void 0);
    exports.Things = Things;
});
@aluanhaddad
Copy link
Contributor

Reproduced in latest nightly.
It simply blows up on the execution of the class declaration with a ReferenceError exception.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 27, 2017

Duplicate of #13449

@mhegazy mhegazy closed this as completed Apr 27, 2017
@mhegazy mhegazy added the Duplicate An existing issue was already created label Apr 27, 2017
@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

3 participants