Skip to content

Compiling class with decorator to ES6 wont load in karma test #3133

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
derek-pavao opened this issue May 12, 2015 · 3 comments
Closed

Compiling class with decorator to ES6 wont load in karma test #3133

derek-pavao opened this issue May 12, 2015 · 3 comments
Labels
Bug A bug in TypeScript Duplicate An existing issue was already created Fixed A PR has been merged for this issue

Comments

@derek-pavao
Copy link

I'm not sure if this is a bug, or I'm missing something.

I am compiling my TypeScript to ES6 and then using the babel preprocessor to run my tests in Karma. The class is as follows

import { BaseModel, notEmpty } from '../main';

export class TestModel extends BaseModel {


    @notEmpty('Test property can not be empty')
    public testProperty: string;


}

The above class compiles to

if (typeof __decorate !== "function") __decorate = function (decorators, target, key, desc) {

    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
    switch (arguments.length) {
        case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
        case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
        case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
    }
};

import { BaseModel, notEmpty } from '../main';
export class TestModel extends BaseModel {
}
__decorate([
    notEmpty('Test property can not be empty')
], TestModel.prototype, "testProperty");

This compiled code doesn't run. Unfortunately I don't get a helpful error either so I'm not exactly sure the problem. However if I mess with the compiled JavaScript a bit

// changing the first line from ...
if (typeof __decorate !== "function") __decorate = function (decorators, target, key, desc) {
    // code here
}

// to this

if (typeof __decorate !== "function") window.__decorate = function (decorators, target, key, desc) {
    // code here
}

// or to this, which would work in both browser and server environments
if (typeof __decorate !== "function") var __decorate = function (decorators, target, key, desc) {
    // code here
}

with either of those changes everything seems to work fine. Like I said though, I'm not sure if its a bug or I'm missing some small thing.

@DanielRosenwasser
Copy link
Member

Hey @dotDeeka, I can't offhandedly pinpoint where the fix occurred, but in release-1.5 I'm getting the following.

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {                                              
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
    switch (arguments.length) {                                                                                                       
        case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);                                   
        case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);                      
        case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);                        
    }                                                                                                                                 
};                                                                                                                                    

I don't think it went out for 1.5 beta, but it should be out for 1.5 proper.

Edit: Updated to reflect that this will be out for 1.5.

@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Fixed A PR has been merged for this issue labels May 14, 2015
@DanielRosenwasser
Copy link
Member

Found it - duplicate of #2836, fixed by #2989.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label May 14, 2015
@derek-pavao
Copy link
Author

@DanielRosenwasser Sorry for the dup, and thanks for the help.

@mhegazy mhegazy added this to the TypeScript 1.5.2 milestone May 18, 2015
@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
Bug A bug in TypeScript 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

3 participants