DEV Community

Cover image for Node Erases My Types but Won't Fix My import Paths
Mustafa ERBAY
Mustafa ERBAY

Posted on Originally published at mustafaerbay.com.tr

Node Erases My Types but Won't Fix My import Paths

There are 35 TypeScript scripts behind this blog: they generate articles, translate them,
render cover images, enforce the source policy. Until now tsx has run every one of them.
Yet Node has supported typing node script.ts since 22.18, and since 24.12 and 25.2 the
feature is not experimental but stable. On 30 November, when
counting which Node line I sit on,
the table came out like this: 22.16.0 on my machine, node-version: 22 in eleven places
across my workflows, and 22.23.5 as the newest patch that line pulls. So my pipeline runs
on a Node that can strip types by itself, while I keep a package in between.

How much work is it to take that package out? The answer did not come from where I
expected. All 35 files cleared the stripping stage without a scratch — not a single
enum, not a single namespace, not one syntax rejection. What broke was the right-hand
side
of the import lines. Twelve files never got past their first line, because Node knows
how to erase a type but not how to fix a path.

Erasing, in the literal sense of blank space

The whole mechanism fits in one sentence. The Node documentation says: "Node.js will
replace inline type annotations with whitespace, and no type checking is performed." The
type annotation is not cut out and discarded; the same number of spaces is put in its
place
.

That is easy to verify: since Node 22.13 the stripping engine is exposed as a function
under node:module. Three small files went in, and the byte length of input and output
came back side by side.

const { stripTypeScriptTypes } = require('node:module');
const src = "export const greet = (name: string): string => 'hello ' + name;\n";
const out = stripTypeScriptTypes(src, { mode: 'strip' });
// out  === "export const greet = (name        )         => 'hello ' + name;\n"
// length: 64 → 64
Enter fullscreen mode Exit fullscreen mode

The second and third files are the two states of the type-import trap described further
down:

// a.ts — 110 bytes

const r: Record = { name: 'x' };
console.log(r.name, VERSION);

// a-fixed.ts — 144 bytes: the first line split in two

const r2: Record = { name: 'z' };
console.log(r2.name, VERSION);
Enter fullscreen mode Exit fullscreen mode
file input (bytes) output (bytes) line count
one-line arrow function 64 64 same
file importing a value and a type 110 110 same
the same file fixed with import type 144 144 same

Byte-for-byte identical in all three — and in all 35 real files in the repository too, with
not one deviation. The line import type { Record } from './types.ts'; turns into a
41-character ribbon of spaces in the output, and the line stays where it was. There is a
price for that, and the docs state it too: "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." If a stack trace says line 418, line 418 of your file really is there.

The invoice for the design comes out of the same place. An engine holding nothing but
whitespace cannot remove any TypeScript feature that requires generating code — and, less
discussed, it looks at one file. It does not know what sits on the other side of an
import line: that file's name, its extension, which names it exports as values and which
as types.

The measuring instrument was wrong on the first try

The first attempt reached for the wrong tool:
node --experimental-strip-types --check scripts/*.ts. The command returned 0, and that
nearly went down as "no problem here." It was wrong for two separate reasons. First,
node --check looks only at its first file argument; the shell may expand twenty-seven
paths, but the rest are silently ignored. Turned into a loop, 27 of 27 passed — and that
meant nothing either: an ESM file with a genuine syntax error inside it comes back from
--check with 0, while the same file throws when it is run. The reverse happens too —
in a file whose module system is determined as CJS, a plain const a: number line blows
--check up, though that file runs fine.

file --check running it
valid TypeScript, ESM 0 0
broken JavaScript, ESM 0 1
enum, ESM 0 1
type annotation, CJS context 1 0

The documentation had already closed this box: "TypeScript syntax is unsupported in the
REPL, --check, and inspect." All twenty-seven passing was telling the truth, but not
out of knowledge; --check was never looking at those files. A measurement that happens to
be right is more dangerous than one that is wrong: nobody opens it a second time. For the
real numbers the work went back to stripTypeScriptTypes above — feeding each file in as a
file, with no module ever executed.

35 files, zero syntax rejections

Handing every .ts file under scripts/ (excluding .d.ts, 35 files) to the stripping
engine produces a one-line result: 35 passed, 0 failed.

That was a surprise, because the list of things stripping refuses is not short. The Node 22
documentation is explicit: "Since Node.js is only removing inline types, any TypeScript
features that involve replacing TypeScript syntax with new JavaScript syntax will error,
unless the flag --experimental-transform-types is passed." The rejected set is enum
declarations, namespaces containing runtime code, constructor parameter properties and

const r: Record = { name: 'x' };


`Record` is an `interface`, `VERSION` is a `const`. The stripping engine cannot tell them
apart: at that moment it is looking only at this file, and it does not see inside
`./types.ts`. So it leaves the line as it stands. Then the ESM linker steps in and finds no
exported *value* named `Record` in the other file:

Enter fullscreen mode Exit fullscreen mode

SyntaxError: The requested module './types.ts' does not provide an export named 'Record'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:182:21)




The docs write this as a rule too: "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 fix is one word:
write `import type { Record }` and the line turns entirely into whitespace, and the file
runs.

This class **never fired** across my 35 files — but it stayed quiet by coincidence, not by
care. The phrase `import type` appears zero times in those files; nobody imported a type,
only called functions. The library side, meanwhile, exports nine types: `IndexedTopic` and
`TopicSimilarity` in `topic-dedup.ts`, `GeminiScope` and `GeminiAttempt` in
`gemini-keys.ts`, `SearchHit` and `SearchOutcome` in `web-search.ts`, `Lang` and
`SpeakerRole` in `elevenlabs.ts`, `AudioSegment` in `audio-mix.ts`. The first line that
imports one of them, in a world without tsx, will be correct TypeScript and broken
JavaScript at the same time.

![Diagram](https://mermaid.ink/img/Zmxvd2NoYXJ0IFRECiAgQVsibm9kZSBzY3JpcHQudHMiXSAtLT4gQlsiTW9kdWxlIHN5c3RlbSBpcyBkZXRlcm1pbmVkPGJyLz4ocGFja2FnZS5qc29uIHR5cGUsIHN5bnRheCkiXQogIEIgLS0-IENbIlR5cGUgc3RyaXBwaW5nOiBhbm5vdGF0aW9uczxici8-YmVjb21lIHNwYWNlcyBvZiBlcXVhbCBsZW5ndGgiXQogIEMgLS0-fCJlbnVtIC8gbmFtZXNwYWNlIC8gcGFyYW1ldGVyIHByb3BlcnR5InwgRFsiRVJSX1VOU1VQUE9SVEVEX1RZUEVTQ1JJUFRfU1lOVEFYIl0KICBDIC0tPiBFWyJTcGVjaWZpZXIgcmVzb2x1dGlvbjxici8-KHRoZSBwYXRoIGlzIHVzZWQgQVMgV1JJVFRFTikiXQogIEUgLS0-fCJ3cml0dGVuIC5qcyAvIG5vIGV4dGVuc2lvbiJ8IEZbIkVSUl9NT0RVTEVfTk9UX0ZPVU5EIl0KICBFIC0tPiBHWyJMaW5raW5nOiBuYW1lcyBsb29rZWQgdXAgaW4gdGhlIG90aGVyIG1vZHVsZSJdCiAgRyAtLT58ImEgdHlwZSB3aXRob3V0IHRoZSB0eXBlIGtleXdvcmQifCBIWyJTeW50YXhFcnJvcjogbm8gZXhwb3J0IG5hbWVkIl0KICBHIC0tPiBJWyJSdW5zIChOTyB0eXBlIGNoZWNraW5nKSJd?type=png&bgColor=white)

Not one of the three red branches in that diagram is type checking. Node does not read
types; it only destroys them. Finding a type error takes a separate compiler run — the
[cache experiment on this blog](https://mustafaerbay.com.tr/en/blog/tutorials/node-compile-cache-tsc-acilisi-yariya-indi-tip-denetimi-kipirdamadi/)
that halved `tsc` startup measured exactly that split: the cache speeds up startup and
leaves type checking itself untouched. In my own pipeline that run does not exist at all:
`package.json` holds a `check` script (`astro check`) and not one workflow calls it. Types
are not being checked today either; tsx leaving would not make that worse, only visible.

## The invoice for removing tsx

The benefit side first, because before measuring it I was overstating it too. Installing
tsx alone into an empty directory brings 3 packages and 11 MB. Median times over ten runs of
a trivial script: bare `node --experimental-strip-types` 55.5 ms, the repository's `tsx`
binary 104.6 ms, and `npx tsx` — the form my workflows actually write — 226.8 ms. That is
~170 ms per call; even if all 18 `npx tsx` calls across seven workflows ran in a single
pass, it would not add up to three seconds. So this is not a speed matter. The gain is three
packages out of the dependency tree and one moving part out of the pipeline.

On the cost side there are three routes. The first is turning twenty specifiers (nineteen static, one
dynamic) into `.ts` by hand: half an hour. But `tsc` will not accept that state with default settings; importing
with a `.ts` extension requires `allowImportingTsExtensions`, which can only be enabled
under `noEmit`/`emitDeclarationOnly`. `rewriteRelativeImportExtensions`, which arrived in
TypeScript 5.7, fills exactly this gap — write `.ts` in the source, emit `.js` in the
output. TypeScript 5.9.3 is installed in the repository, so the option is within reach;
`tsconfig.json`, though, extends `astro/tsconfigs/strict` and ends in three lines. The block
the Node docs recommend for 5.8 and later carries six settings: `noEmit`, `target: esnext`,
`module: nodenext`, `rewriteRelativeImportExtensions`, `erasableSyntaxOnly`,
`verbatimModuleSyntax`. So it is not "fix twenty lines", it is "rebuild the compiler
configuration".

The second route is turning the limit itself into a rule. `erasableSyntaxOnly` does
precisely that: it makes every non-erasable TypeScript construct a compile-time error. There
are no violations today, but writing an `enum` takes one person one minute — and the flag
that would have rescued that `enum` is gone.

The third is leaving tsx where it is. The measurement actually showed what tsx does for me,
and that job is not stripping types: running the same files under tsx 4.23.15, all three
broken classes went through without a complaint. The `.ts` file written as `.js` opened, the
extensionless import resolved, even the line importing a type name as a value ran. tsx's
real contribution is patching the resolver.

What sets the order for my own pipeline is a different calendar. In that article I promised to move to Node 26 before February 2027; when that move happens type stripping will
be a stable feature, the `--experimental-transform-types` escape will be gone entirely, and
`tsconfig` `paths` aliases will — in the current docs' words — "won't be transformed and
will therefore produce an error". So the sequence should follow the calendar rather than the
price tag: pin the boundary with `erasableSyntaxOnly` first (a gain independent of the
move), do the 26 migration, and fold the `.ts` extensions and
`rewriteRelativeImportExtensions` into that same migration. Dropping tsx from `package.json`
is the last step, because it means changing 20 call sites (18 `npx tsx` across seven
workflows, 2 `node --import tsx` in `package.json`) on the same day.

The limits of the measurement: the scan covers only `scripts/`, while the 50 `.ts` files
under `src/` (again excluding `.d.ts`) are compiled by Astro/Vite and never handed to Node
directly. The resolution check asks whether a file exists on disk; whether npm packages are
installed, and the single dynamic import above, did not enter the numbers. End-to-end
verification was done with two scripts, not twelve — running the other ten would have meant
posting to social media, generating articles, writing files. There is one more wrinkle on
the version side: `node-version: 22` does not pin the patch, and the docs say the stripper's
output should not be considered stable across versions. The same commit can run under two
different parsers on two different days.

## Closing

When you are about to lift a tool out of its place, the question is not "is this job in the
platform now" but "which part of that job did the platform take over". Node took over type
stripping and does it flawlessly: 35 files, zero syntax rejections, line numbers preserved
byte for byte, no source map bill. The part it did not take over is the module graph. `tsx`
was not wrestling with my repository's types; it was translating paths, and because it did
that job quietly for years, nobody ever had to write the real extension of a library file.

The beauty of erasing with whitespace and its limit come from the same place. A design that
preserves line numbers cannot do any work that changes length, and translating a path
changes length. That is why Node's TypeScript support is not a compiler substitute but a
*syntax* substitute. The 20 specifiers in the repository are the invoice for it: today tsx
pays it, tomorrow I will.

## Official Sources

- [Node.js v22 documentation, Modules: TypeScript](https://nodejs.org/docs/latest-v22.x/api/typescript.html) — type stripping was "Added in: v22.6.0", and the history table has two entries for `v22.18.0`: "Type stripping no longer emits an experimental warning" and "Type stripping is enabled by default". On this page the stability level is still "1.2 - Release candidate". The mechanism: "Node.js will replace inline type annotations with whitespace, and no type checking is performed"; the source-map rationale: "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". The rejected-syntax rule in full: "Since Node.js is only removing inline types, any TypeScript features that involve _replacing_ TypeScript syntax with new JavaScript syntax will error, unless the flag `--experimental-transform-types` is passed". The extension rule: "As in JavaScript files, file extensions are mandatory in `import` statements and `import()` expressions: `import './file.ts'`, not `import './file'`". The type-import rule: "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". Also: "TypeScript syntax is unsupported in the REPL, `--check`, and `inspect`".
- [Node.js current documentation (v26.14.0), Modules: TypeScript](https://nodejs.org/docs/latest/api/typescript.html) — stability is now "Stability: 2 - Stable". The history table has five rows: "v26.0.0 | Removed `--experimental-transform-types` flag", "v25.2.0, v24.12.0 | Type stripping is now stable", "v24.3.0, v22.18.0 | Type stripping no longer emits an experimental warning", "v23.6.0, v22.18.0 | Type stripping is enabled by default", "v22.7.0 | Added `--experimental-transform-types` flag". In the current wording of the same sentence the "unless the flag…" clause is absent. On path aliases: "`tsconfig` \"paths\" won't be transformed and will therefore produce an error. The closest feature available is subpath imports with the limitation that they need to start with `#`"; and "Node.js ignores `tsconfig.json` files". The recommended block carries six settings: `noEmit`, `target: esnext`, `module: nodenext`, `rewriteRelativeImportExtensions`, `erasableSyntaxOnly`, `verbatimModuleSyntax`.
- [Node.js v22 documentation, `module.stripTypeScriptTypes(code[, options])`](https://nodejs.org/docs/latest-v22.x/api/module.html) — "Added in: v22.13.0". The `mode` default is `'strip'`: "Only strip type annotations without performing the transformation of TypeScript features". Source-map behaviour: "When mode is `'strip'`, source maps are not generated, because locations are preserved". The documented example output is `const a         = 1;`. Warning: "The output of this function should not be considered stable across Node.js versions, due to changes in the TypeScript parser".
- [nodejs/amaro repository (README)](https://github.com/nodejs/amaro) — "Amaro is a Node.js wrapper around the SWC Next TypeScript type stripper" and "It's used as an internal in Node.js for Type Stripping but can also be used as a standalone package". Today's mode: "Amaro supports only `strip-only`" together with "The former `transform` mode and `amaro/transform` loader are no longer available". Rejected syntax: "Syntax that requires runtime code generation, such as enums and constructor parameter properties, is rejected". The stack-trace rationale: "Stack traces are preserved, by replacing removed types with white spaces".
- [TypeScript, TSConfig reference](https://www.typescriptlang.org/tsconfig/) — `rewriteRelativeImportExtensions` (5.7): "Rewrite `.ts`, `.tsx`, `.mts`, and `.cts` file extensions in relative import paths to their JavaScript equivalent in output files". `allowImportingTsExtensions` (5.0) permits imports with a `.ts` extension but is "only allowed when `--noEmit` or `--emitDeclarationOnly` is enabled, since these import paths would not be resolvable at runtime in JavaScript output files". `erasableSyntaxOnly` "will cause TypeScript to error on most TypeScript-specific constructs that have runtime behavior". `verbatimModuleSyntax` (5.0): "any imports or exports without a `type` modifier are left around. Anything that uses the `type` modifier is dropped entirely".
- [Node.js release index (`index.json`)](https://nodejs.org/dist/index.json) — the dates named in this article: `v22.6.0` 2024-08-06, `v22.7.0` 2024-08-21, `v22.13.0` 2025-01-06, `v22.16.0` 2025-05-20, `v22.18.0` 2025-07-31, `v24.12.0` 2025-12-10, `v25.2.0` 2025-11-11, `v26.0.0` 2026-05-05. On the day of measurement the newest patch on the 22 line is `v22.23.5` (2026-11-11, LTS "Jod"), and the newest LTS overall is `v26.14.0` (2026-11-30, LTS "Lithium").
Enter fullscreen mode Exit fullscreen mode

Top comments (0)