See More

{ "type": "module", "source": "doc/api/typescript.md", "modules": [ { "textRaw": "Modules: TypeScript", "name": "modules:_typescript", "introduced_in": "v22.6.0", "type": "module", "meta": { "changes": [ { "version": "v26.0.0", "pr-url": "https://github.com/nodejs/node/pull/61803", "description": "Removed `--experimental-transform-types` flag." }, { "version": [ "v25.2.0", "v24.12.0" ], "pr-url": "https://github.com/nodejs/node/pull/60600", "description": "Type stripping is now stable." }, { "version": [ "v24.3.0", "v22.18.0" ], "pr-url": "https://github.com/nodejs/node/pull/58643", "description": "Type stripping no longer emits an experimental warning." }, { "version": [ "v23.6.0", "v22.18.0" ], "pr-url": "https://github.com/nodejs/node/pull/56350", "description": "Type stripping is enabled by default." }, { "version": "v22.7.0", "pr-url": "https://github.com/nodejs/node/pull/54283", "description": "Added `--experimental-transform-types` flag." } ] }, "stability": 2, "stabilityText": "Stable", "modules": [ { "textRaw": "Enabling", "name": "enabling", "type": "module", "desc": "

There are two ways to enable runtime TypeScript support in Node.js:

\n
    \n
  1. \n

    For full support of all of TypeScript's syntax and features, including\nusing any version of TypeScript, use a third-party package.

    \n
  2. \n
  3. \n

    For lightweight support, you can use the built-in support for\ntype stripping.

    \n
  4. \n
", "displayName": "Enabling" }, { "textRaw": "Full TypeScript support", "name": "full_typescript_support", "type": "module", "desc": "

To use TypeScript with full support for all TypeScript features, including\ntsconfig.json, you can use a third-party package. These instructions use\ntsx as an example but there are many other similar libraries available.

\n
    \n
  1. \n

    Install the package as a development dependency using whatever package\nmanager you're using for your project. For example, with npm:

    \n
    npm install --save-dev tsx\n
    \n
  2. \n
  3. \n

    Then you can run your TypeScript code via:

    \n
    npx tsx your-file.ts\n
    \n

    Or alternatively, you can run with node via:

    \n
    node --import=tsx your-file.ts\n
    \n
  4. \n
", "displayName": "Full TypeScript support" }, { "textRaw": "Type stripping", "name": "type_stripping", "type": "module", "meta": { "added": [ "v22.6.0" ], "changes": [ { "version": [ "v25.2.0", "v24.12.0" ], "pr-url": "https://github.com/nodejs/node/pull/60600", "description": "Type stripping is now stable." } ] }, "desc": "

By default Node.js will execute TypeScript files that contains only\nerasable TypeScript syntax.\nNode.js will replace TypeScript syntax with whitespace,\nand no type checking is performed.\nTo disable this feature, use the flag --no-strip-types.

\n

Node.js ignores tsconfig.json files and therefore\nfeatures that depend on settings within tsconfig.json,\nsuch as paths or converting newer JavaScript syntax to older standards, are\nintentionally unsupported. To get full TypeScript support, see Full TypeScript support.

\n

The type stripping feature is designed to be lightweight.\nBy intentionally not supporting syntaxes that require JavaScript code\ngeneration, and by replacing inline types with whitespace, Node.js can run\nTypeScript code without the need for source maps.

\n

Type stripping is compatible with most versions of TypeScript\nbut we recommend version 5.8 or newer with the following tsconfig.json settings:

\n
{\n  \"compilerOptions\": {\n     \"noEmit\": true, // Optional - see note below\n     \"target\": \"esnext\",\n     \"module\": \"nodenext\",\n     \"rewriteRelativeImportExtensions\": true,\n     \"erasableSyntaxOnly\": true,\n     \"verbatimModuleSyntax\": true\n  }\n}\n
\n

Use the noEmit option if you intend to only execute *.ts files, for example\na build script. You won't need this flag if you intend to distribute *.js\nfiles.

", "modules": [ { "textRaw": "Determining module system", "name": "determining_module_system", "type": "module", "desc": "

Node.js supports both CommonJS and ES Modules syntax in TypeScript\nfiles. Node.js will not convert from one module system to another; if you want\nyour code to run as an ES module, you must use import and export syntax, and\nif you want your code to run as CommonJS you must use require and\nmodule.exports.

\n\n

As in JavaScript files, file extensions are mandatory in import statements\nand import() expressions: import './file.ts', not import './file'. Because\nof backward compatibility, file extensions are also mandatory in require()\ncalls: require('./file.ts'), not require('./file'), similar to how the\n.cjs extension is mandatory in require calls in CommonJS files.

\n

The tsconfig.json option allowImportingTsExtensions will allow the\nTypeScript compiler tsc to type-check files with import specifiers that\ninclude the .ts extension.

", "displayName": "Determining module system" }, { "textRaw": "TypeScript features", "name": "typescript_features", "type": "module", "desc": "

Since Node.js is only removing inline types, any TypeScript features that\ninvolve replacing TypeScript syntax with new JavaScript syntax will error.

\n

The most prominent features that require transformation are:

\n\n

namespaces that do not contain runtime code are supported.\nThis example will work correctly:

\n
// This namespace is exporting a type\nnamespace TypeOnly {\n   export type A = string;\n}\n
\n

This will result in ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX error:

\n
// This namespace is exporting a value\nnamespace A {\n   export let x = 1\n}\n
\n

Since Decorators are currently a TC39 Stage 3 proposal,\nthey are not transformed and will result in a parser error.\nNode.js does not provide polyfills and thus will not support decorators until\nthey are supported natively in JavaScript.

\n

In addition, Node.js does not read tsconfig.json files and does not support\nfeatures that depend on settings within tsconfig.json, such as paths or\nconverting newer JavaScript syntax into older standards.

", "displayName": "TypeScript features" }, { "textRaw": "Importing types without `type` keyword", "name": "importing_types_without_`type`_keyword", "type": "module", "desc": "

Due to the nature of type stripping, the type keyword is necessary to\ncorrectly strip type imports. Without the type keyword, Node.js will treat the\nimport as a value import, which will result in a runtime error. The tsconfig\noption verbatimModuleSyntax can be used to match this behavior.

\n

This example will work correctly:

\n
import type { Type1, Type2 } from './module.ts';\nimport { fn, type FnParams } from './fn.ts';\n
\n

This will result in a runtime error:

\n
import { Type1, Type2 } from './module.ts';\nimport { fn, FnParams } from './fn.ts';\n
", "displayName": "Importing types without `type` keyword" }, { "textRaw": "Non-file forms of input", "name": "non-file_forms_of_input", "type": "module", "desc": "

Type stripping can be enabled for --eval and STDIN. The module system\nwill be determined by --input-type, as it is for JavaScript.

\n

TypeScript syntax is unsupported in the REPL, --check, and\ninspect.

", "displayName": "Non-file forms of input" }, { "textRaw": "Source maps", "name": "source_maps", "type": "module", "desc": "

Since inline types are replaced by whitespace, source maps are unnecessary for\ncorrect line numbers in stack traces; and Node.js does not generate them.

", "displayName": "Source maps" }, { "textRaw": "Type stripping in dependencies", "name": "type_stripping_in_dependencies", "type": "module", "desc": "

To discourage package authors from publishing packages written in TypeScript,\nNode.js refuses to handle TypeScript files inside folders under a node_modules\npath.

", "displayName": "Type stripping in dependencies" }, { "textRaw": "Paths aliases", "name": "paths_aliases", "type": "module", "desc": "

tsconfig \"paths\" won't be transformed and therefore produce an error. The closest\nfeature available is subpath imports with the limitation that they need to start\nwith #.

", "displayName": "Paths aliases" } ], "displayName": "Type stripping" } ], "displayName": "Modules: TypeScript" } ] }