1

Closed

TS1.0: Imported files/modules are not included in generated define even when contained types are referenced.

description

I am using AngularJS with dynamic loading using RequireJS. Even though I am declaring properties in a constructor which has property parameters using types defined in the imported files/modules, the compiler does not include the modules in the generated define function call. I wind up having to add an extra reference to the module, even though I have declared a property that uses a type in the module, within my constructor:
/// <reference path="../_all.ts" />

import app = require("app");
import baseController = require('baseController');
import dataServiceModule = require('services/dataService');
dataServiceModule; // ***************I should not have to include this************
module MyApp {
    'use strict';

    export class SomeController extends baseController.PartialPageController {
        // static $injector accommodates the fact that parameters may be renamed during minification.
        static $inject = ['$scope', '$routeParams', 'globalState', '$window', 'dataService'];

        // the constructor has a reference to the dataServiceModule but the compiler doesn't recognize this for import purposes.
        constructor($scope: ng.IScope, $routeParams,$window, private service: dataServiceModule.someDataService) {
            super($scope, $routeParams, $window);
        }

    }

    app.controllerProvider.register('SomeController', ["$scope", "$routeParams", "$window", "dataService", SomeController]);
}
Closed by

comments

RyanCavanaugh wrote Apr 14, 2014 at 6:18 PM

This is intentional - unless dataServiceModule is used in a value position, the compiler optimizes away the reference.

If you still want it included (say, for its side effects), you can use the amd-dependency tag. See https://typescript.codeplex.com/discussions/439314

** Closed by RyanCavanaugh 04/14/2014 10:18AM

rcollette wrote Apr 15, 2014 at 12:33 PM

Maybe it is just me, but what optimization is there to be made? If I am using a concrete type that is defined in the imported module, then the module must be physically loaded in order to use the concrete type.

In what scenario would a concrete type be referenced as I have done above, but the module would not need to be loaded?

This scenario deserves further consideration.

SelFC wrote Oct 30, 2016 at 7:50 AM

When I explicitly load the module with import it shouldn't be skipped by the compiler or at least I should have an option in "compilerOptions" to specify if I want to optimize in this manner or not.
Sometimes you need to load a module which does not export anything but is global and then you need to do this workaround:

import myMod from 'myMod'

myMod;

Which is just a hack. "amd-dependency" is deprecated now so we don't have any supported way of doing it. I just think that compiler shouldn't try to be smarter then a developer and make decisions for him (its just a tool) At least we should have a choice there.