<p dir="auto">Is there an api that can be <code class="notranslate">require</code> ed from node to compile file. eg</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="var ts = require('typescript');
ts.compile(filePath, {options}, function(err, js) {
 //etc
})"><pre class="notranslate"><span class="pl-k">var</span> <span class="pl-s1">ts</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'typescript'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-s1">ts</span><span class="pl-kos">.</span><span class="pl-en">compile</span><span class="pl-kos">(</span><span class="pl-s1">filePath</span><span class="pl-kos">,</span> <span class="pl-kos">{</span>options<span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-k">function</span><span class="pl-kos">(</span><span class="pl-s1">err</span><span class="pl-kos">,</span> <span class="pl-s1">js</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
 <span class="pl-c">//etc</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span></pre></div>
<p dir="auto">I'm trying to use generic class in the TSX file and generic class is implemented in the another module. I have tried to use code like in the <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="96398755" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/3960" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/3960/hovercard?comment_id=144529141&amp;comment_type=issue_comment" href="https://github.com/microsoft/TypeScript/issues/3960#issuecomment-144529141">#3960 (comment)</a> but it doesn't work because old style casting is not allowed in the TSX file. Ok, I have changed it to the <code class="notranslate">as</code> operator but it emits incorrect code. It refers to the generic class directly without using <code class="notranslate">require</code>.</p>
<p dir="auto">After that I have created simple example without JSX which demonstrates that old style casting and <code class="notranslate">as</code> operator are different.</p>
<p dir="auto">File <strong>generic.ts</strong>: <code class="notranslate">export default class Generic&lt;T&gt; {}</code></p>
<p dir="auto">File <strong>sample.ts</strong>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import Generic from &quot;./generic&quot;;
type Ctor = new() =&gt; Generic&lt;number&gt;;
const x = Generic as Ctor;"><pre class="notranslate"><code class="notranslate">import Generic from "./generic";
type Ctor = new() =&gt; Generic&lt;number&gt;;
const x = Generic as Ctor;
</code></pre></div>
<p dir="auto">Compile <code class="notranslate">tsc sample.ts --module commonjs</code>. Result:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="var generic_1 = require(&quot;./generic&quot;);
var x = Generic;"><pre class="notranslate"><code class="notranslate">var generic_1 = require("./generic");
var x = Generic;
</code></pre></div>
<p dir="auto">As you see generic_1 variable isn't used at all and undeclared <code class="notranslate">Generic</code> variable is assigned to <code class="notranslate">x</code>.</p>
<p dir="auto">Workaround:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import Generic from &quot;./generic&quot;;
type Ctor = new () =&gt; Generic&lt;number&gt;;
const x = Generic;
const y = x as Ctor;"><pre class="notranslate"><code class="notranslate">import Generic from "./generic";
type Ctor = new () =&gt; Generic&lt;number&gt;;
const x = Generic;
const y = x as Ctor;
</code></pre></div>
<p dir="auto">Also please note that using old-style casting also helps:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import Generic from &quot;./generic&quot;;
type Ctor = new() =&gt; Generic&lt;number&gt;;
const x = &lt;Ctor&gt;Generic;"><pre class="notranslate"><code class="notranslate">import Generic from "./generic";
type Ctor = new() =&gt; Generic&lt;number&gt;;
const x = &lt;Ctor&gt;Generic;
</code></pre></div>
<p dir="auto">So it looks like it is the problem in the <code class="notranslate">as</code> operator only.</p>