Source Files Dependencies - when to use what?

Topics: General
Oct 3, 2012 at 11:39 AM

What exactly is the difference between reference comments and import declarations, e.g.:

///<reference path='./myclass.ts' />


import MyClass= module("./myclass");

I can see that the compiler output differs, but I'm not really sure whether one solution is to be preferred over the other.

Thank you!

Oct 3, 2012 at 11:59 PM

When you do an import, you are implying that you will be using a module system (such as require.js for AMD modules, or node for CommonJS modules) to load separate files containing the code you are importing (typically, e.g. myclass.js above).  TypeScript also injects the code to do this based on the module system (e.g. var MyClass = require(...)).

When you use the <reference...> syntax, you are effectively including another TypeScript file into your current file that you know will be available at runtime (e.g. already included by another script tag, or because you will compile with the --out switch to concatenate the files at compile time).

Effectively in both cases you're saying "this other code will be available at runtime", but in the import case you are use a module loader to load it (and TypeScript generates code to do so), and in the <reference> case, you control how the other file is going to be present.

Oct 7, 2012 at 10:39 PM

Hi - could you please describe how you add the --out switch to the compile time?

I unloaded the project, edited the csproj file to add --out like so:

<Target Name="BeforeBuild">
<Exec Command="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript\\tsc --out&quot; @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />

But it errors with a Build Failure code 9009.

Oct 7, 2012 at 11:14 PM

Sorry my stupidity, after some more trial and error it should look like this:

<Exec Command="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript\\tsc&quot; --out single.js @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />

Where single.js is whatever filename you want to give the final single output.