Skip to content

Commit 8cb595a

Browse files
authored
docs: clarify TypeScript type stripping behavior (#76)
1 parent 994b48d commit 8cb595a

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

pages/typescript/run-natively.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ authors: AugustinMauroy, khaosdoctor, jakebailey, robpalme
66

77
You can write code that's valid TypeScript directly in Node.js without the need to transpile it first.
88

9+
Node.js runs TypeScript through a lightweight process called type stripping. It removes erasable TypeScript syntax, such as type annotations and interfaces, then runs the remaining JavaScript.
10+
911
If you are using v22.18.0 or later and your source code contains only [erasable TypeScript syntax](https://devblogs.microsoft.com/typescript/announcing-typescript-5-8-beta/#the---erasablesyntaxonly-option), you can execute TypeScript code without any flags.
1012

1113
```bash
@@ -18,7 +20,13 @@ If you are using a version less than v22.18.0, you can use the `--experimental-s
1820
node --experimental-strip-types example.ts
1921
```
2022

21-
And that's it! You can now run TypeScript code directly in Node.js without the need to transpile it first, and use TypeScript to catch type-related errors.
23+
And that's it! You can now run TypeScript code directly in Node.js without the need to transpile it first.
24+
25+
Node.js does not type check your code when it runs TypeScript files. Use the TypeScript compiler separately if you want to catch type-related errors:
26+
27+
```bash
28+
npx tsc --noEmit
29+
```
2230

2331
You can disable it via [`--no-experimental-strip-types`](https://nodejs.org/docs/latest-v22.x/api/cli.html#--no-experimental-strip-types) flag if needed.
2432

@@ -40,6 +48,18 @@ The support for TypeScript in Node.js has some constraints to keep in mind:
4048

4149
You can get more information on the [API docs](https://nodejs.org/docs/latest-v22.x/api/typescript.html#typescript-features).
4250

51+
### Type stripping
52+
53+
Type stripping only works for TypeScript syntax that can be removed without changing the runtime JavaScript. This includes common type-only syntax such as type annotations, interfaces, type aliases, and `import type`.
54+
55+
Syntax that requires JavaScript code generation is not handled by type stripping alone. Examples include `enum`, parameter properties, namespaces with runtime code, and import aliases. Use `--experimental-transform-types`, a runner, or a separate transpilation step if your project needs those features.
56+
57+
### Type checking
58+
59+
Running a `.ts` file with `node` is not the same as running `tsc`. Node.js executes the file after stripping supported type syntax, but it does not report type errors.
60+
61+
For development, a common setup is to run Node.js directly for quick feedback and run `tsc --noEmit` in a separate command or CI job for type checking.
62+
4363
### Configuration
4464

4565
The Node.js TypeScript loader ([Amaro](https://github.com/nodejs/amaro)) does not need or use `tsconfig.json` to run TypeScript code.

0 commit comments

Comments
 (0)