{"$schema":"https://doc-kit.nodejs.org/schemas/api-doc/1.0.0.json","id":"typescript","path":"/typescript","type":"module","module":"typescript","title":"Modules: TypeScript","introducedIn":"v22.6.0","sourceLink":null,"stability":{"index":"2","description":"Stable"},"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[{"versions":["v26.0.0"],"prUrl":"https://github.com/nodejs/node/pull/61803","commit":null,"description":"Removed `--experimental-transform-types` flag."},{"versions":["v25.2.0","v24.12.0"],"prUrl":"https://github.com/nodejs/node/pull/60600","commit":null,"description":"Type stripping is now stable."},{"versions":["v24.3.0","v22.18.0"],"prUrl":"https://github.com/nodejs/node/pull/58643","commit":null,"description":"Type stripping no longer emits an experimental warning."},{"versions":["v23.6.0","v22.18.0"],"prUrl":"https://github.com/nodejs/node/pull/56350","commit":null,"description":"Type stripping is enabled by default."},{"versions":["v22.7.0"],"prUrl":"https://github.com/nodejs/node/pull/54283","commit":null,"description":"Added `--experimental-transform-types` flag."}],"description":"","summary":"","examples":[],"children":[{"kind":"section","id":"enabling","name":"Enabling","title":"Enabling","scope":"module","overloadOf":null,"stability":null,"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"There are two ways to enable runtime TypeScript support in Node.js:\n\n1. For [full support](#full-typescript-support) of all of TypeScript's syntax and features, including\n   using any version of TypeScript, use a third-party package.\n\n2. For lightweight support, you can use the built-in support for\n   [type stripping](#type-stripping).","summary":"There are two ways to enable runtime TypeScript support in Node.js:","examples":[],"children":[]},{"kind":"section","id":"full-typescript-support","name":"Full TypeScript support","title":"Full TypeScript support","scope":"module","overloadOf":null,"stability":null,"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"To use TypeScript with full support for all TypeScript features, including\n`tsconfig.json`, you can use a third-party package. These instructions use\n[`tsx`](https://tsx.hirok.io/) as an example but there are many other similar libraries available.\n\n1. Install the package as a development dependency using whatever package\n   manager you're using for your project. For example, with `npm`:\n\n   ```bash\n   npm install --save-dev tsx\n   ```\n\n2. Then you can run your TypeScript code via:\n\n   ```bash\n   npx tsx your-file.ts\n   ```\n\n   Or alternatively, you can run with `node` via:\n\n   ```bash\n   node --import=tsx your-file.ts\n   ```","summary":"To use TypeScript with full support for all TypeScript features, including `tsconfig.json`, you can use a third-party package. These instructions use `tsx` as an example but there are many other similar libraries available.","examples":[{"language":"bash","displayName":null,"code":"npm install --save-dev tsx"},{"language":"bash","displayName":null,"code":"npx tsx your-file.ts"},{"language":"bash","displayName":null,"code":"node --import=tsx your-file.ts"}],"children":[]},{"kind":"section","id":"type-stripping","name":"Type stripping","title":"Type stripping","scope":"module","overloadOf":null,"stability":null,"added":["v22.6.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[{"versions":["v25.2.0","v24.12.0"],"prUrl":"https://github.com/nodejs/node/pull/60600","commit":null,"description":"Type stripping is now stable."}],"description":"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`](cli.html#--no-strip-types).\n\nNode.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](#full-typescript-support).\n\nThe 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\nType stripping is compatible with most versions of TypeScript\nbut we recommend version 5.8 or newer with the following `tsconfig.json` settings:\n\n```json\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\nUse 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.","summary":"By default Node.js will execute TypeScript files that contains only erasable TypeScript syntax. Node.js will replace TypeScript syntax with whitespace, and no type checking is performed. To disable this feature, use the flag `--no-strip-types`.","examples":[{"language":"json","displayName":null,"code":"{\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}"}],"children":[{"kind":"section","id":"determining-module-system","name":"Determining module system","title":"Determining module system","scope":"module","overloadOf":null,"stability":null,"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"Node.js supports both [CommonJS](modules.html) and [ES Modules](esm.html) 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\n`module.exports`.\n\n* `.ts` files will have their module system determined [the same way as `.js`\n  files.](packages.html#determining-module-system) To use `import` and `export` syntax, add `\"type\": \"module\"` to the\n  nearest parent `package.json`.\n* `.mts` files will always be run as ES modules, similar to `.mjs` files.\n* `.cts` files will always be run as CommonJS modules, similar to `.cjs` files.\n* `.tsx` files are unsupported.\n\nAs in JavaScript files, [file extensions are mandatory](esm.html#mandatory-file-extensions) 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\nThe `tsconfig.json` option `allowImportingTsExtensions` will allow the\nTypeScript compiler `tsc` to type-check files with `import` specifiers that\ninclude the `.ts` extension.","summary":"Node.js supports both CommonJS and ES Modules 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 `import` and `export` syntax, and if you want your code to run as CommonJS you must use `require` and `module.exports`.","examples":[],"children":[]},{"kind":"section","id":"typescript-features","name":"TypeScript features","title":"TypeScript features","scope":"module","overloadOf":null,"stability":null,"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"Since Node.js is only removing inline types, any TypeScript features that\ninvolve *replacing* TypeScript syntax with new JavaScript syntax will error.\n\nThe most prominent features that require transformation are:\n\n* `Enum` declarations\n* `namespace` with runtime code\n* parameter properties\n* import aliases\n\n`namespace`s that do not contain runtime code are supported.\nThis example will work correctly:\n\n```ts\n// This namespace is exporting a type\nnamespace TypeOnly {\n   export type A = string;\n}\n```\n\nThis will result in [`ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX`](errors.html#err_unsupported_typescript_syntax) error:\n\n```ts\n// This namespace is exporting a value\nnamespace A {\n   export let x = 1\n}\n```\n\nSince Decorators are currently a [TC39 Stage 3 proposal](https://github.com/tc39/proposal-decorators),\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\nIn 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.","summary":"Since Node.js is only removing inline types, any TypeScript features that involve _replacing_ TypeScript syntax with new JavaScript syntax will error.","examples":[{"language":"ts","displayName":null,"code":"// This namespace is exporting a type\nnamespace TypeOnly {\n   export type A = string;\n}"},{"language":"ts","displayName":null,"code":"// This namespace is exporting a value\nnamespace A {\n   export let x = 1\n}"}],"children":[]},{"kind":"section","id":"importing-types-without-type-keyword","name":"Importing types without type keyword","title":"Importing types without `type` keyword","scope":"module","overloadOf":null,"stability":null,"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"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`](https://www.typescriptlang.org/tsconfig/#verbatimModuleSyntax) can be used to match this behavior.\n\nThis example will work correctly:\n\n```ts\nimport type { Type1, Type2 } from './module.ts';\nimport { fn, type FnParams } from './fn.ts';\n```\n\nThis will result in a runtime error:\n\n```ts\nimport { Type1, Type2 } from './module.ts';\nimport { fn, FnParams } from './fn.ts';\n```","summary":"Due to the nature of type stripping, the `type` keyword is necessary to correctly strip type imports. Without the `type` keyword, Node.js will treat the import as a value import, which will result in a runtime error. The tsconfig option `verbatimModuleSyntax` can be used to match this behavior.","examples":[{"language":"ts","displayName":null,"code":"import type { Type1, Type2 } from './module.ts';\nimport { fn, type FnParams } from './fn.ts';"},{"language":"ts","displayName":null,"code":"import { Type1, Type2 } from './module.ts';\nimport { fn, FnParams } from './fn.ts';"}],"children":[]},{"kind":"section","id":"non-file-forms-of-input","name":"Non-file forms of input","title":"Non-file forms of input","scope":"module","overloadOf":null,"stability":null,"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"Type stripping can be enabled for `--eval` and STDIN. The module system\nwill be determined by `--input-type`, as it is for JavaScript.\n\nTypeScript syntax is unsupported in the REPL, `--check`, and\n`inspect`.","summary":"Type stripping can be enabled for `--eval` and STDIN. The module system will be determined by `--input-type`, as it is for JavaScript.","examples":[],"children":[]},{"kind":"section","id":"source-maps","name":"Source maps","title":"Source maps","scope":"module","overloadOf":null,"stability":null,"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"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.","summary":"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.","examples":[],"children":[]},{"kind":"section","id":"type-stripping-in-dependencies","name":"Type stripping in dependencies","title":"Type stripping in dependencies","scope":"module","overloadOf":null,"stability":null,"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"To discourage package authors from publishing packages written in TypeScript,\nNode.js refuses to handle TypeScript files inside folders under a `node_modules`\npath.","summary":"To discourage package authors from publishing packages written in TypeScript, Node.js refuses to handle TypeScript files inside folders under a `node_modules` path.","examples":[],"children":[]},{"kind":"section","id":"paths-aliases","name":"Paths aliases","title":"Paths aliases","scope":"module","overloadOf":null,"stability":null,"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"[`tsconfig` \"paths\"](https://www.typescriptlang.org/tsconfig/#paths) won't be transformed and therefore produce an error. The closest\nfeature available is [subpath imports](packages.html#subpath-imports) with the limitation that they need to start\nwith `#`.","summary":"`tsconfig` \"paths\" won't be transformed and therefore produce an error. The closest feature available is subpath imports with the limitation that they need to start with `#`.","examples":[],"children":[]}]}]}