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

[Emitter] Wrong design:type metadata for decorated fields #4364

Closed
pcan opened this issue Aug 19, 2015 · 4 comments
Closed

[Emitter] Wrong design:type metadata for decorated fields #4364

pcan opened this issue Aug 19, 2015 · 4 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@pcan
Copy link
Contributor

pcan commented Aug 19, 2015

When I compile this code:

class A {
    constructor() { console.log('new A'); }
}

type AType = A;

function decorator(target: Object, propertyKey: string) {
}

export class B {

    @decorator
    x: AType = new A();
}

The emitter produces the following code:

var A = (function () {
    function A() {
        console.log('new A');
    }
    return A;
})();
function decorator(target, propertyKey) {
}
var B = (function () {
    function B() {
        this.x = new A();
    }
    __decorate([
        decorator, 
        __metadata('design:type', Object)
    ], B.prototype, "x");
    return B;
})();
exports.B = B;

But the expected output for metadata should be __metadata('design:type', A). Furthermore, if I change the param declaration to x: A = new A();, the emitter will output __metadata('design:type', ) (type not specified at all).

@fopsdev
Copy link

fopsdev commented Aug 19, 2015

i think i'm running into the same issue here:

import {bindable} from 'aurelia-framework';

export class NavBar {
  @bindable router = null ;
}

gets translated to:

...
        __decorate([
            aurelia_framework_1.bindable, 
            __metadata('design:type', )
...

@mhegazy
Copy link
Contributor

mhegazy commented Aug 19, 2015

This is by design. the intended behavior is not doable for the general case. one thing to keep in mind is we are serializing the type using the constructor function. if you alias it, the constructor function might not be, and probably is not, in scope.

consider this:

// a.ts
class A {
}

export type TypeA = A;
export function getA() {  return new A(); } 

and

// b.ts
import {AType, getA} from "a";
export class B {
    @decorator
    x: AType = getA(); // 'A' is not in scope here, the value will be undefined.
}

Solutions, either you use import aliasForA = A, which will generate a variable aliasForA = A so the serialization would work, or not use type aliases.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 19, 2015

@fopsdev this is a different issue. i will file a different bug to track it

@wwwsevolod
Copy link

@mhegazy what about @fopsdev issue? can't find created bug ;( im stuck with this bug too =
but little bit different:

class TestAction extends ActionCreator {
    dispatch(a: number) {
        return a;
    }
}

function InjectAction(target: any, a: any) {
    console.log(target);
    console.log(typeof a);
    // Reflect.getMetadata('design:type', target)
}

class Test extends Component<any, any> {
    @InjectAction
    testAction: TestAction;

    render() {
        return <div>
            123
        </div>;
    }
}

produces

   40 | __decorate([
   41 |     InjectAction, 
> 42 |     __metadata('design:type', )
    |                               ^
   43 | ], Test.prototype, "testAction");

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Aug 24, 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 Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

6 participants