12

I am trying to use Typescript and jspm to make an angular app. The problem is when you want to ensure a .js file loaded, in jspm you have to write an import and it ensures the file will load before running your code. But Typescript removes my import. This is the Typescript code I have written. I have to load angular-new-router then add it to my module dependency.

import angular = require('angular');
import MainController = require('./controllers/MainController');
import NgNewRoute = require('angular-new-router');

console.log(angular.version);

var appModule = angular.module('app', ['ngNewRouter']);
MainController.register(appModule);

export = appModule;

My question: How can I instruct Typescript to not remove my import statement, Or I have to do something else to ensure my router loads?

PS: I compile my typescript code to ES5 with commonjs.

EDIT: This question is not the same as TypeScript: import module with only statements. I have this problem working with third party libraries, so I don't want to change them. Also I am using commonjs pattern so amd-dependency does not fix my problem!

EDIT 2: Another problem is I can not require files other than js modules in my Typescript code.

Community
  • 1
  • 1
alisabzevari
  • 8,008
  • 6
  • 43
  • 67
  • possible duplicate of [TypeScript: import module with only statements](http://stackoverflow.com/questions/16455211/typescript-import-module-with-only-statements) – sainaen Jun 21 '15 at 05:48
  • 1
    @sainaen: That is somehow related to my question but not completely. I am requiring third party libraries so that's not a good idea to change them to export a variable or sth. – alisabzevari Jun 21 '15 at 06:07
  • 1
    Yes, the accepted answer on that question is not the best one. See the other one, with more votes, it has a link to the official issue tracker with response from one the TypeScript team members and two possible workarounds, like it suggests that just referencing the imported module without doing anything with it may help. – sainaen Jun 21 '15 at 06:11

1 Answers1

4

How can I instruct Typescript to not remove my import statement, Or I have to do something else to ensure my router loads

You need to use something from the import as a variable e.g

import foo = require('./foo'); 
var bar = foo; // Like this

instead of just :

import foo = require('./foo'); 
var bar:foo; // This will not cause an import in the generated JavaScript
basarat
  • 261,912
  • 58
  • 460
  • 511
  • 1
    This is to support the use case of *lazy loading* : http://basarat.gitbooks.io/typescript/content/docs/project/external-modules.html – basarat Jul 14 '15 at 07:02
  • I can't understand the relation of your answer to the provided link. The lazy loading use case is related to conditional loading of a type, but I want my to have angular library loaded before running it; Or I want to load a css file before my code runs. – alisabzevari Jul 14 '15 at 10:25