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
> **⚠️WARNING⚠️:** All content in this article uses Node.js experimental features. Please make sure you are using a version of Node.js that supports the features mentioned in this article. And remember that experimental features can change in future versions of Node.js.
@@ -14,18 +14,37 @@ In the previous articles, we learned how to run TypeScript code using transpilat
14
14
15
15
Since V22.6.0, Node.js has experimental support for some TypeScript syntax via "type stripping". You can write code that's valid TypeScript directly in Node.js without the need to transpile it first.
16
16
17
-
The `--experimental-strip-types` flag tells Node.js to strip the type annotations from the TypeScript code before running it.
17
+
The [`--experimental-strip-types`](https://nodejs.org/docs/latest-v22.x/api/cli.html#--experimental-strip-types) flag tells Node.js to strip the type annotations from the TypeScript code before running it.
18
18
19
19
```bash
20
20
node --experimental-strip-types example.ts
21
21
```
22
22
23
23
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.
24
24
25
-
In V22.7.0 this experimental support was extended to transform TypeScript-only syntax, like `enum`s and `namespace`, with the addition of the `--experimental-transform-types` flag.
25
+
In V22.7.0 this experimental support was extended to transform TypeScript-only syntax, like `enum`s and `namespace`, with the addition of the [`--experimental-transform-types`](https://nodejs.org/docs/latest-v23.x/api/cli.html#--experimental-transform-types) flag. Enabling `--experimental-transform-types` automatically implies that `--experimental-strip-types` is enabled, so there's no need to use both flags in the same command:
From V23 onwards, the `--experimental-strip-types` flag is enabled by default (you can disable it via the [`--no-experimental-strip-types`](https://nodejs.org/docs/latest-v23.x/api/cli.html#--no-experimental-strip-types) flag), enabling you to run any supported syntax, so running files like the one below with `node file.ts` is supported:
32
+
33
+
```ts
34
+
function foo(bar:number):string {
35
+
return'hello';
36
+
}
37
+
```
38
+
39
+
However, running any code that requires transformations, like the code below still needs the use of `--experimental-transform-types`:
40
+
41
+
```ts
42
+
enumMyEnum {
43
+
A,
44
+
B,
45
+
}
46
+
47
+
console.log(MyEnum.A);
29
48
```
30
49
31
50
Future versions of Node.js will include support for TypeScript without the need for a command line flag.
@@ -34,7 +53,13 @@ Future versions of Node.js will include support for TypeScript without the need
34
53
35
54
At the time of writing, the experimental support for TypeScript in Node.js has some limitations.
36
55
37
-
You can get more information on the [API docs](https://nodejs.org/docs/latest/api/typescript.html#typescript-features).
56
+
You can get more information on the [API docs](https://nodejs.org/docs/latest-v23.x/api/typescript.html#typescript-features).
57
+
58
+
### Configuration
59
+
60
+
The Node.js TypeScript loader ([Amaro](https://github.com/nodejs/amaro)) does not need or use `tsconfig.json` to run TypeScript code.
61
+
62
+
We recommend configuring your editor and `tsc` to reflect Node.js behavior by creating a `tsconfig.json` using the `compilerOptions` listed [here](https://nodejs.org/api/typescript.html#type-stripping), as well as using TypeScript version **5.7 or higher**.
0 commit comments