Targeting CommonJS Modules with TypeScript

2013-08-18
TypeScript

Seeing as most of the code I’m writing is targeting the Node.js platform I want the TypeScript compiler to generate JavaScript that works with CommonJS.

I’ve been reading articles on TypeScript but they seem to be pre version 0.9 and the way of telling the compiler what to do seems to have changed. Rather than the following code in the Visual Studio project file…

&lt;<span style="color: #2b91af;">Exec</span> <span style="color: #2b91af;">Command</span>=<span style="color: #800000;">"&amp;quot;$(PROGRAMFILES)Microsoft SDKsTypeScripttsc&amp;quot; </span>`<span style="color: #800000;">--module commonjs @(TypeScriptCompile -&gt;'&amp;quot;%(fullpath)&amp;quot;', ' ')"</span> /&gt;</pre>
...you actually need the following code now...
<pre>` <span style="color: #800000;">&lt;PropertyGroup</span> <span style="color: #ff0000;">Condition</span>=<span style="color: #0000ff;">"'$(Configuration)' == 'Debug'"</span><span style="color: #800000;">&gt;</span>
<span style="color: #800000;">&lt;TypeScriptTarget&gt;</span>ES3<span style="color: #800000;">&lt;/TypeScriptTarget&gt;</span>
<span style="color: #800000;">&lt;TypeScriptIncludeComments&gt;</span>true<span style="color: #800000;">&lt;/TypeScriptIncludeComments&gt;</span>
<span style="color: #800000;">&lt;TypeScriptSourceMap&gt;</span>true<span style="color: #800000;">&lt;/TypeScriptSourceMap&gt;</span>
<span style="color: #800000;">&lt;TypeScriptModuleKind&gt;</span>CommonJS<span style="color: #800000;">&lt;/TypeScriptModuleKind&gt;</span>
 <span style="color: #800000;">&lt;/PropertyGroup&gt;</span>
<span style="color: #800000;">&lt;PropertyGroup</span> <span style="color: #ff0000;">Condition</span>=<span style="color: #0000ff;">"'$(Configuration)' == 'Release'"</span><span style="color: #800000;">&gt;</span>
<span style="color: #800000;">&lt;TypeScriptTarget&gt;</span>ES3<span style="color: #800000;">&lt;/TypeScriptTarget&gt;</span>
<span style="color: #800000;">&lt;TypeScriptIncludeComments&gt;</span>false<span style="color: #800000;">&lt;/TypeScriptIncludeComments&gt;</span>
<span style="color: #800000;">&lt;TypeScriptSourceMap&gt;</span>false<span style="color: #800000;">&lt;/TypeScriptSourceMap&gt;</span>
<span style="color: #800000;">&lt;TypeScriptModuleKind&gt;</span>CommonJS<span style="color: #800000;">&lt;/TypeScriptModuleKind&gt;</span>
<span style="color: #800000;">&lt;/PropertyGroup&gt;</span>
 <span style="color: #800000;">&lt;Import</span> <span style="color: #ff0000;">Project</span>=<span style="color: #0000ff;">"$(VSToolsPath)TypeScriptMicrosoft.TypeScript.targets"</span> <span style="color: #800000;">/&gt;</span>

Comments: