deerchao deerchao - 4 months ago 41
TypeScript Question

TypeScript: import module with only statements

I have a

which would compile into

alert('this is from page-a');

And I have a
which compiles into

import pageA = module('page-a')
alert('this is from main');

And this is my
command line:

tsc --module amd page-a.ts main.ts

And I'm using
like this:

<script src="require.js" data-main="main.js"></script>

I can't see the alert messagebox from
when loading the page. And in the generated scripts
, there is nothing about

My question is, why is this happening? And how do I force typescript to import a module that is not explicitly used by the code?


Your generated js will not import a module not explicitly used in code. Typescript is clever that way and will not generate any js for a module that is only imported and not used.

You need to :

  • export a variable in the first module AND
  • import the first module in the second module AND
  • Use the exported variable in the second module

so have page-a.js something like:

export var x = 123; // just to export something that you can use
alert('this is from page-a');

and main.ts as :

import pageA = module('page-a')
var foo = pageA.x; // If you remove this line and you will not get the alert from page-a
alert('this is from main');