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

invalid drop of require statement although there is a reference to it in emitted js code, #12888

Closed
dqweek opened this issue Dec 13, 2016 · 2 comments · Fixed by #12890
Closed
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@dqweek
Copy link

dqweek commented Dec 13, 2016

TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)

Code

file: auxilry.ts

export class SomeClass {
    field: string;
}

file: main.ts

import { SomeClass } from './auxiliry';

function annotation(): ClassDecorator {
    return (target: any, key: string | symbol): void => { };
}

@annotation()
export class ClassA {
    array: SomeClass[];

    constructor(...init: SomeClass[]) {
        this.array = init;
    }
}

Expected behavior:

runtime should not throw exception

Actual behavior:

exception thrown on execution:

Uncaught ReferenceError: auxiliry_1 is not defined

additional data (compiled js):

"use strict";
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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
function annotation() {
    return function (target, key) { };
}
var ClassA = (function () {
    function ClassA() {
        var init = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            init[_i] = arguments[_i];
        }
        this.array = init.slice();
    }
    return ClassA;
}());
ClassA = __decorate([
    annotation,
    __metadata("design:paramtypes", [typeof (_a = typeof auxiliry_1.SomeClass !== "undefined" && auxiliry_1.SomeClass) === "function" && _a || Object])
], ClassA); /// <-- as I can see the require statement is dropped from emitted code, although, there is a reference to it here.
exports.ClassA = ClassA;
var _a;

probagrelated to #596

@dqweek dqweek changed the title ReferenceError: auxiliry_1 is not defined invalid drop of require statement although there is a reference to it in emitted js code, Dec 13, 2016
@vladima vladima added the Bug A bug in TypeScript label Dec 13, 2016
@mhegazy mhegazy added this to the TypeScript 2.2 milestone Dec 13, 2016
@vladima vladima added the Fixed A PR has been merged for this issue label Dec 13, 2016
@dqweek
Copy link
Author

dqweek commented Jan 12, 2017

after upgrading to TypeScript 2.1.5.
the issue regressed in different place.

when 'strictNullCheck' option is false the following typescript emitt invalid javascript code.

file: auxiliry.ts

export class SomeClass {
    field: string;
}

file: main.ts

import { SomeClass } from './auxiliry';

function annotation(): PropertyDecorator {
    return (target: any): void => { };
}

export class ClassA {
    @annotation() array: SomeClass | null;
}

Emitted code:

"use strict";
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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
function annotation() {
    return function (target) { };
}
var ClassA = (function () {
    function ClassA() {
    }
    return ClassA;
}());
__decorate([
    annotation(),
    __metadata("design:type", auxiliry_1.SomeClass) // <-- auxiliry_1 is not emitted as require statement
], ClassA.prototype, "array", void 0);
exports.ClassA = ClassA;

@mhegazy
Copy link
Contributor

mhegazy commented Jan 12, 2017

Filed #13449 to track it.

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

Successfully merging a pull request may close this issue.

3 participants