Skip to content

Require been ignord for emitDecoratorMetadata if module is unused and name is lowercase #4239

@tjoskar

Description

@tjoskar

If an imported module is unused it will be ignored, which is good (It may exist edge cases where this is bad but that is an other discussion).

import {db} from './db'
console.log('Hello Dexter Morgan');

Compiles to:

console.log('Hello Dexter Morgan');

(One can of course, import './db' to keep it).

But this becomes a problem if I use emitDecoratorMetadata and a decorator:

import {db} from './db';

function someDecorator(target) {
    return target;
}

@someDecorator
class MyClass {
    db: db;

    constructor(db: db) {
        this.db = db;
        this.db.doSomething();
    }
}

export {MyClass};

This compiles to:

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);
    }
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
function someDecorator(target) {
    return target;
}
var MyClass = (function () {
    function MyClass(db) {
        this.db = db;
        this.db.doSomething();
    }
    MyClass = __decorate([
        someDecorator, 
        __metadata('design:paramtypes', [db_1.db]) // db_1 is not defined
    ], MyClass);
    return MyClass;
})();
exports.MyClass = MyClass;

And if a run this code I get ReferenceError: db_1 is not defined.

The funny thing is however that this code works:

import {db as Database} from './db';

function someDecorator(target) {
    return target;
}

@someDecorator
class MyClass {
    db: Database;

    constructor(db: Database) {
        this.db = db;
        this.db.doSomething();
    }

}

export {MyClass}

If I import the database class with import {Database} from './db'; it works as well. So it seams like the compiler ignore to import a module if its name is in lower case?

I'm using version 1.6.0-dev.20150808 of tsc.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions