Skip to content

File import ignored if the file containing the import statement doesn't explicitely reference a class deifined in the file #5098

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
SpenceDiNicolantonio opened this issue Oct 3, 2015 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@SpenceDiNicolantonio
Copy link

Class MyClass is defined in my-class.ts. The class is decorated with a custom decorator. The result of the decorator is some logic that will eventually cause MyClass to be instantiated.

Another file, other-file.ts, imports my-class.ts with:

import 'my-class';

other-file.ts has no explicit reference to MyClass, as it will automatically be instantiated and used as needed as a result of its decorator.

This works fine, until I try to import an interface, exported by my-class.ts

If I change the import within other-file.ts to:

import {ISomeInterface} from 'my-class'`;

I can now reference ISomeInterface within other-file.ts, but now the contents of my-class.ts is never actually compiled.

I suspect this is due to some optimization by the Typescript compiler. In general it would make sense that if nothing but interfaces were referenced from a class, there's no reason to generate javascript for it; however, the use of decorators makes that assumption erroneous.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 5, 2015

@SpenceDiNicolantonio can you provide more data, i can not get this to reproduce locally:

import {B} from "./module2";

export function decorate(c:any): void{
    // use the metadata from import B
}

@decorate
export class C {
    constructor(a: B) {
    }
}

tsc module.ts --experimentalDecorators --emitDecoratorMetadata --m commonjs --t es5

var module2_1 = require("./module2");

function decorate(c) {
    // use the metadata from import B
}
exports.decorate = decorate;
var C = (function () {
    function C(a) {
    }
    C = __decorate([
        decorate, 
        __metadata('design:paramtypes', [module2_1.B])
    ], C);
    return C;
})();
exports.C = C;

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Oct 5, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Oct 5, 2015

And to answer your question, the compiler will elide imports that are only used in a type position. see @danquirk's answer in #4717 (comment) for more information. if any of these imports are used by a decorator, the import will not be elided.

@SpenceDiNicolantonio SpenceDiNicolantonio changed the title File import ignored if no the file containing the import statement doesn't explicitely reference a class deifined in the file File import ignored if the file containing the import statement doesn't explicitely reference a class deifined in the file Oct 25, 2015
@mhegazy mhegazy added Question An issue which isn't directly actionable in code and removed Needs More Info The issue still hasn't been fully clarified labels Nov 13, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Nov 13, 2015

please reopen if #5098 (comment) does not answer the original question.

@mhegazy mhegazy closed this as completed Nov 13, 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
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants