Server IP : 85.214.239.14 / Your IP : 3.144.93.10 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/self/root/proc/2/root/proc/3/cwd/proc/3/root/usr/share/doc/nodejs/api/ |
Upload File : |
<!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.13.1"> <title>Modules: TypeScript | Node.js v22.13.1 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"> <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.13.1 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.13.1</li> <li class="picker-header"> <a href="#"> <span class="picker-arrow"></span> Table of contents </a> <div class="picker"><div class="toc"><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></div></div> </li> <li class="picker-header"> <a href="#"> <span class="picker-arrow"></span> Index </a> <div class="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="#"> <span class="picker-arrow"></span> Options </a> <div class="picker"> <ul> <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>. 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, STDIN input, <code>--print</code>, <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>