-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type self-reference in class decorator uses incorrect local variables in generated JS. #9685
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
Comments
looks like the same issue as #4521 |
@mhegazy I think the issue is different. The previous post refers to eliminating the need to use things like forwardRef. In this issue I am not arguing for that. Instead it shows how forwardRef fails altogether which will have big implications for Angular 2, something that is a breaking change from 1.8.10. Thanks! |
i see the issue now. thanks for the clarification. this is a bug that we should fix. |
Awesome, thanks! |
FYI this is not limited to decorators it turns out, but any self reference such as accessing a static member of the same type. Simple work-around is to declare a module local variable, use it in place of the type reference, then set the variable to the type underneath the class. |
@EricABC could you give an example of what you mean by this, " any self reference such as accessing a static member of the same type." do you mean something like: declare var Something1: any;
@Something1
export class MyClass {
static prop0 = " hi"
static prop1 = () => MyClass.prop0
} |
Sure, here is one: export class Widget {
static color: string = "green";
doStuff(): void {
console.log(Widget.color);
}
} A simple work-around is this: var typeRefThingy: any;
export class Widget {
static color: string = "green";
doStuff(): void {
console.log(typeRefThingy.color);
}
}
typeRefThingy = Widget; Any occurrence of Widget that is not after its definition will result in the same issue. |
@EricABC Thanks for clarification. I can't repo your second example though with following configuration Let me know if I miss something here original export class Widget {
static color: string = "green";
doStuff(): void {
console.log(Widget.color);
}
} output; this looks correct System.register([], function(exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var Widget;
return {
setters:[],
execute: function() {
Widget = class Widget {
doStuff() {
console.log(Widget.color);
}
};
Widget.color = "green";
exports_1("Widget", Widget);
}
}
}); |
TypeScript Version: 2.0.0
The problem is a common occurrence in Angular 2 where forwardRef is often used. It is included in the example, but removing forwardRef and the angular dependency will produce the same issue.
Code
Expected behavior:
Actual behavior:
The real-world example that worked in 1.8.10 is here:
TSConfig targeting systemjs and es6
The text was updated successfully, but these errors were encountered: