You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can write code that's valid TypeScript directly in Node.js without the need to transpile it first.
8
8
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
+
9
11
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.
10
12
11
13
```bash
@@ -18,7 +20,13 @@ If you are using a version less than v22.18.0, you can use the `--experimental-s
18
20
node --experimental-strip-types example.ts
19
21
```
20
22
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
+
```
22
30
23
31
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.
24
32
@@ -40,6 +48,18 @@ The support for TypeScript in Node.js has some constraints to keep in mind:
40
48
41
49
You can get more information on the [API docs](https://nodejs.org/docs/latest-v22.x/api/typescript.html#typescript-features).
42
50
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
+
43
63
### Configuration
44
64
45
65
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