Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Module Systems

Node.js module.stripTypeScriptTypes() method

Reference for module.stripTypeScriptTypes() in Node.js: turn TypeScript source into JavaScript, strip versus transform mode, source maps and unsupported syntax.

module.stripTypeScriptTypes(code) takes TypeScript source text and returns JavaScript with the type annotations removed. It is the same transformation Node applies when it runs a .ts file directly, exposed as a function so tools can use it on strings.

In the default "strip" mode the function replaces every type with blank space, so the output has the same length and every remaining token keeps its original line and column. That is why stack traces still point at the right place without a source map.

NODE
import { stripTypeScriptTypes } from 'node:module';

console.log(stripTypeScriptTypes('const n: number = 1;'));

Try it

Node.js Stripping types keeps positions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Transform mode, source maps and unsupported syntax

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
import { stripTypeScriptTypes } from 'node:module';

stripTypeScriptTypes(code: string, options?: {
  mode?: 'strip' | 'transform',
  sourceMap?: boolean,
  sourceUrl?: string
}): string
Name Description
code TypeScript source text (module or script).
options.mode 'strip' (default) only erases type syntax. 'transform' also rewrites syntax that has runtime meaning, such as enum, namespace and constructor parameter properties.
options.sourceMap In transform mode, append an inline source map (//# sourceMappingURL=data:...). Not allowed in strip mode.
options.sourceUrl A name appended as //# sourceURL=... so debuggers label the code. Ignored when sourceMap is on (the map carries the name).

Returns: a JavaScript string. In strip mode it is exactly as long as the input and contains the same lines.

Errors: ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX in strip mode when the code uses syntax that cannot be erased (for example enum), ERR_INVALID_TYPESCRIPT_SYNTAX for code that does not parse, and ERR_INVALID_ARG_VALUE for bad options.

Usage notes

  • It does not type-check. Wrong types pass silently; only the syntax is examined. Use tsc --noEmit (or the TypeScript 7 compiler) for checking.
  • Erasable syntax only in strip mode. Annotations, interface, type, generics, as, satisfies, ! assertions and import type are fine. enum, namespace with values, parameter properties and import x = require() are not.
  • Trailing spaces are the types. The blanks left behind are what keeps columns aligned. Trim them only for display, as the first example does.
  • Import specifiers are not rewritten. import './a.ts' stays as written; Node resolves real .ts files. Use import type for type-only imports so they disappear.
  • Parser limits. Very new TypeScript syntax can be rejected until Node's bundled parser (Amaro) catches up.
  • It is still experimental. Node 24 prints ExperimentalWarning: stripTypeScriptTypes is an experimental feature on stderr the first time you call it, as both examples show; behavior may change between releases.
  • Not a build step for libraries. It does not touch tsconfig.json, path aliases or decorators.