1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta name="nodejs.org:node-version" content="v22.14.0">
<title>Modules: TypeScript | Node.js v22.14.0 Documentation</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/hljs.css">
<link rel="canonical" href="https://nodejs.org/api/typescript.html">
<script async defer src="assets/api.js" type="text/javascript"></script>
<script>
const storedTheme = localStorage.getItem('theme');
// Follow operating system theme preference
if (storedTheme === null && window.matchMedia) {
const mq = window.matchMedia('(prefers-color-scheme: dark)');
if (mq.matches) {
document.documentElement.classList.add('dark-mode');
}
} else if (storedTheme === 'dark') {
document.documentElement.classList.add('dark-mode');
}
</script>
</head>
<body class="alt apidoc" id="api-section-typescript">
<a href="#apicontent" class="skip-to-content">Skip to content</a>
<div id="content" class="clearfix">
<div role="navigation" id="column2" class="interior">
<div id="intro" class="interior">
<a href="/" title="Go back to the home page">
Node.js
</a>
</div>
<ul>
<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File system</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
<li><a href="typescript.html" class="nav-typescript active">Modules: TypeScript</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="report.html" class="nav-report">Report</a></li>
<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
<li><a href="sqlite.html" class="nav-sqlite">SQLite</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
<li><a href="test.html" class="nav-test">Test runner</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
</ul>
</div>
<div id="column1" data-id="typescript" class="interior">
<header class="header">
<div class="header-container">
<h1>Node.js v22.14.0 documentation</h1>
<button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
<svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
<path fill="none" d="M0 0h24v24H0z" />
<path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
<path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
<path d="M0 0h24v24H0z" fill="none" />
<path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
</svg>
</button>
</div>
<div id="gtoc">
<ul>
<li class="pinned-header">Node.js v22.14.0</li>
<li class="picker-header">
<a href="#toc-picker" aria-controls="toc-picker">
<span class="picker-arrow"></span>
Table of contents
</a>
<div class="picker" tabindex="-1"><div class="toc"><ul id="toc-picker">
<li><span class="stability_1"><a href="#modules-typescript">Modules: TypeScript</a></span>
<ul>
<li><a href="#enabling">Enabling</a></li>
<li><a href="#full-typescript-support">Full TypeScript support</a></li>
<li><span class="stability_1"><a href="#type-stripping">Type stripping</a></span>
<ul>
<li><a href="#determining-module-system">Determining module system</a></li>
<li><a href="#typescript-features">TypeScript features</a></li>
<li><a href="#importing-types-without-type-keyword">Importing types without <code>type</code> keyword</a></li>
<li><a href="#non-file-forms-of-input">Non-file forms of input</a></li>
<li><a href="#source-maps">Source maps</a></li>
<li><a href="#type-stripping-in-dependencies">Type stripping in dependencies</a></li>
<li><a href="#paths-aliases">Paths aliases</a></li>
</ul>
</li>
</ul>
</li>
</ul></div></div>
</li>
<li class="picker-header">
<a href="#gtoc-picker" aria-controls="gtoc-picker">
<span class="picker-arrow"></span>
Index
</a>
<div class="picker" tabindex="-1" id="gtoc-picker"><ul>
<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
<li>
<a href="index.html">Index</a>
</li>
</ul>
<hr class="line">
<ul>
<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File system</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
<li><a href="typescript.html" class="nav-typescript active">Modules: TypeScript</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="report.html" class="nav-report">Report</a></li>
<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
<li><a href="sqlite.html" class="nav-sqlite">SQLite</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
<li><a href="test.html" class="nav-test">Test runner</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
</ul></div>
</li>
<li class="picker-header">
<a href="#options-picker" aria-controls="options-picker">
<span class="picker-arrow"></span>
Options
</a>
<div class="picker" tabindex="-1">
<ul id="options-picker">
<li>
<a href="all.html">View on single page</a>
</li>
<li>
<a href="typescript.json">View as JSON</a>
</li>
<li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/typescript.md">Edit on GitHub</a></li>
</ul>
</div>
</li>
</ul>
</div>
<hr>
</header>
<details role="navigation" id="toc" open><summary>Table of contents</summary><ul>
<li><span class="stability_1"><a href="#modules-typescript">Modules: TypeScript</a></span>
<ul>
<li><a href="#enabling">Enabling</a></li>
<li><a href="#full-typescript-support">Full TypeScript support</a></li>
<li><span class="stability_1"><a href="#type-stripping">Type stripping</a></span>
<ul>
<li><a href="#determining-module-system">Determining module system</a></li>
<li><a href="#typescript-features">TypeScript features</a></li>
<li><a href="#importing-types-without-type-keyword">Importing types without <code>type</code> keyword</a></li>
<li><a href="#non-file-forms-of-input">Non-file forms of input</a></li>
<li><a href="#source-maps">Source maps</a></li>
<li><a href="#type-stripping-in-dependencies">Type stripping in dependencies</a></li>
<li><a href="#paths-aliases">Paths aliases</a></li>
</ul>
</li>
</ul>
</li>
</ul></details>
<div role="main" id="apicontent">
<h2>Modules: TypeScript<span><a class="mark" href="#modules-typescript" id="modules-typescript">#</a></span><a aria-hidden="true" class="legacy" id="typescript_modules_typescript"></a></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v22.7.0</td>
<td><p>Added <code>--experimental-transform-types</code> flag.</p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a>.1 - Active development</div><p></p>
<section><h3>Enabling<span><a class="mark" href="#enabling" id="enabling">#</a></span><a aria-hidden="true" class="legacy" id="typescript_enabling"></a></h3>
<p>There are two ways to enable runtime TypeScript support in Node.js:</p>
<ol>
<li>
<p>For <a href="#full-typescript-support">full support</a> of all of TypeScript's syntax and features, including
using any version of TypeScript, use a third-party package.</p>
</li>
<li>
<p>For lightweight support, you can use the built-in support for
<a href="#type-stripping">type stripping</a>.</p>
</li>
</ol>
</section><section><h3>Full TypeScript support<span><a class="mark" href="#full-typescript-support" id="full-typescript-support">#</a></span><a aria-hidden="true" class="legacy" id="typescript_full_typescript_support"></a></h3>
<p>To use TypeScript with full support for all TypeScript features, including
<code>tsconfig.json</code>, you can use a third-party package. These instructions use
<a href="https://tsx.is/"><code>tsx</code></a> as an example but there are many other similar libraries available.</p>
<ol>
<li>
<p>Install the package as a development dependency using whatever package
manager you're using for your project. For example, with <code>npm</code>:</p>
<pre><code class="language-bash">npm install --save-dev tsx</code> <button class="copy-button">copy</button></pre>
</li>
<li>
<p>Then you can run your TypeScript code via:</p>
<pre><code class="language-bash">npx tsx your-file.ts</code> <button class="copy-button">copy</button></pre>
<p>Or alternatively, you can run with <code>node</code> via:</p>
<pre><code class="language-bash">node --import=tsx your-file.ts</code> <button class="copy-button">copy</button></pre>
</li>
</ol>
</section><section><h3>Type stripping<span><a class="mark" href="#type-stripping" id="type-stripping">#</a></span><a aria-hidden="true" class="legacy" id="typescript_type_stripping"></a></h3>
<div class="api_metadata">
<span>Added in: v22.6.0</span>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a>.1 - Active development</div><p></p>
<p>The flag <a href="cli.html#--experimental-strip-types"><code>--experimental-strip-types</code></a> enables Node.js to run TypeScript
files. By default Node.js will execute only files that contain no
TypeScript features that require transformation, such as enums or namespaces.
Node.js will replace inline type annotations with whitespace,
and no type checking is performed.
To enable the transformation of such features
use the flag <a href="cli.html#--experimental-transform-types"><code>--experimental-transform-types</code></a>.
TypeScript features that depend on settings within <code>tsconfig.json</code>,
such as paths or converting newer JavaScript syntax to older standards, are
intentionally unsupported. To get full TypeScript support, see <a href="#full-typescript-support">Full TypeScript support</a>.</p>
<p>The type stripping feature is designed to be lightweight.
By intentionally not supporting syntaxes that require JavaScript code
generation, and by replacing inline types with whitespace, Node.js can run
TypeScript code without the need for source maps.</p>
<p>Type stripping works with most versions of TypeScript
but we recommend version 5.7 or newer with the following <code>tsconfig.json</code> settings:</p>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
<span class="hljs-attr">"compilerOptions"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">"target"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"esnext"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"module"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"nodenext"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"allowImportingTsExtensions"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"rewriteRelativeImportExtensions"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"verbatimModuleSyntax"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
<h4>Determining module system<span><a class="mark" href="#determining-module-system" id="determining-module-system">#</a></span><a aria-hidden="true" class="legacy" id="typescript_determining_module_system"></a></h4>
<p>Node.js supports both <a href="modules.html">CommonJS</a> and <a href="esm.html">ES Modules</a> syntax in TypeScript
files. Node.js will not convert from one module system to another; if you want
your code to run as an ES module, you must use <code>import</code> and <code>export</code> syntax, and
if you want your code to run as CommonJS you must use <code>require</code> and
<code>module.exports</code>.</p>
<ul>
<li><code>.ts</code> files will have their module system determined <a href="packages.html#determining-module-system">the same way as <code>.js</code>
files.</a> To use <code>import</code> and <code>export</code> syntax, add <code>"type": "module"</code> to the
nearest parent <code>package.json</code>.</li>
<li><code>.mts</code> files will always be run as ES modules, similar to <code>.mjs</code> files.</li>
<li><code>.cts</code> files will always be run as CommonJS modules, similar to <code>.cjs</code> files.</li>
<li><code>.tsx</code> files are unsupported.</li>
</ul>
<p>As in JavaScript files, <a href="esm.html#mandatory-file-extensions">file extensions are mandatory</a> in <code>import</code> statements
and <code>import()</code> expressions: <code>import './file.ts'</code>, not <code>import './file'</code>. Because
of backward compatibility, file extensions are also mandatory in <code>require()</code>
calls: <code>require('./file.ts')</code>, not <code>require('./file')</code>, similar to how the
<code>.cjs</code> extension is mandatory in <code>require</code> calls in CommonJS files.</p>
<p>The <code>tsconfig.json</code> option <code>allowImportingTsExtensions</code> will allow the
TypeScript compiler <code>tsc</code> to type-check files with <code>import</code> specifiers that
include the <code>.ts</code> extension.</p>
<h4>TypeScript features<span><a class="mark" href="#typescript-features" id="typescript-features">#</a></span><a aria-hidden="true" class="legacy" id="typescript_typescript_features"></a></h4>
<p>Since Node.js is only removing inline types, any TypeScript features that
involve <em>replacing</em> TypeScript syntax with new JavaScript syntax will error,
unless the flag <a href="cli.html#--experimental-transform-types"><code>--experimental-transform-types</code></a> is passed.</p>
<p>The most prominent features that require transformation are:</p>
<ul>
<li><code>Enum</code></li>
<li><code>namespaces</code></li>
<li><code>legacy module</code></li>
<li>parameter properties</li>
</ul>
<p>Since Decorators are currently a <a href="https://github.com/tc39/proposal-decorators">TC39 Stage 3 proposal</a>
and will soon be supported by the JavaScript engine,
they are not transformed and will result in a parser error.
This is a temporary limitation and will be resolved in the future.</p>
<p>In addition, Node.js does not read <code>tsconfig.json</code> files and does not support
features that depend on settings within <code>tsconfig.json</code>, such as paths or
converting newer JavaScript syntax into older standards.</p>
<h4>Importing types without <code>type</code> keyword<span><a class="mark" href="#importing-types-without-type-keyword" id="importing-types-without-type-keyword">#</a></span><a aria-hidden="true" class="legacy" id="typescript_importing_types_without_type_keyword"></a></h4>
<p>Due to the nature of type stripping, the <code>type</code> keyword is necessary to
correctly strip type imports. Without the <code>type</code> keyword, Node.js will treat the
import as a value import, which will result in a runtime error. The tsconfig
option <a href="https://www.typescriptlang.org/tsconfig/#verbatimModuleSyntax"><code>verbatimModuleSyntax</code></a> can be used to match this behavior.</p>
<p>This example will work correctly:</p>
<pre><code class="language-ts"><span class="hljs-keyword">import</span> <span class="hljs-keyword">type</span> { <span class="hljs-title class_">Type1</span>, <span class="hljs-title class_">Type2</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'./module.ts'</span>;
<span class="hljs-keyword">import</span> { fn, <span class="hljs-keyword">type</span> <span class="hljs-title class_">FnParams</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'./fn.ts'</span>;</code> <button class="copy-button">copy</button></pre>
<p>This will result in a runtime error:</p>
<pre><code class="language-ts"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Type1</span>, <span class="hljs-title class_">Type2</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'./module.ts'</span>;
<span class="hljs-keyword">import</span> { fn, <span class="hljs-title class_">FnParams</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'./fn.ts'</span>;</code> <button class="copy-button">copy</button></pre>
<h4>Non-file forms of input<span><a class="mark" href="#non-file-forms-of-input" id="non-file-forms-of-input">#</a></span><a aria-hidden="true" class="legacy" id="typescript_non_file_forms_of_input"></a></h4>
<p>Type stripping can be enabled for <code>--eval</code> and STDIN. The module system
will be determined by <code>--input-type</code>, as it is for JavaScript.</p>
<p>TypeScript syntax is unsupported in the REPL, <code>--check</code>, and
<code>inspect</code>.</p>
<h4>Source maps<span><a class="mark" href="#source-maps" id="source-maps">#</a></span><a aria-hidden="true" class="legacy" id="typescript_source_maps"></a></h4>
<p>Since inline types are replaced by whitespace, source maps are unnecessary for
correct line numbers in stack traces; and Node.js does not generate them.
When <a href="cli.html#--experimental-transform-types"><code>--experimental-transform-types</code></a> is enabled, source-maps
are enabled by default.</p>
<h4>Type stripping in dependencies<span><a class="mark" href="#type-stripping-in-dependencies" id="type-stripping-in-dependencies">#</a></span><a aria-hidden="true" class="legacy" id="typescript_type_stripping_in_dependencies"></a></h4>
<p>To discourage package authors from publishing packages written in TypeScript,
Node.js will by default refuse to handle TypeScript files inside folders under
a <code>node_modules</code> path.</p>
<h4>Paths aliases<span><a class="mark" href="#paths-aliases" id="paths-aliases">#</a></span><a aria-hidden="true" class="legacy" id="typescript_paths_aliases"></a></h4>
<p><a href="https://www.typescriptlang.org/tsconfig/#paths"><code>tsconfig</code> "paths"</a> won't be transformed and therefore produce an error. The closest
feature available is <a href="packages.html#subpath-imports">subpath imports</a> with the limitation that they need to start
with <code>#</code>.</p></section>
<!-- API END -->
</div>
</div>
</div>
</body>
</html>
|