Skip to content

Commit 95047b1

Browse files
khaosdoctorAugustinMauroyrobpalme
authored
doc: update typescript docs (#7412)
* doc: update typescript docs Fix a small conceptual issue and add details for v23 Signed-off-by: Lucas Santos <lhs.santoss@gmail.com> * doc: fix small issues * doc: add suggestions to improve clarity * doc: lint authors Co-authored-by: Augustin Mauroy <augustin.mauroy@outlook.fr> Signed-off-by: Lucas Santos <lhs.santoss@gmail.com> * doc: apply changes Co-authored-by: Rob Palmer <rob.palmer2@gmail.com> Signed-off-by: Lucas Santos <lhs.santoss@gmail.com> * doc: nits and pin versions * doc: add Rob --------- Signed-off-by: Lucas Santos <lhs.santoss@gmail.com> Co-authored-by: Augustin Mauroy <augustin.mauroy@outlook.fr> Co-authored-by: Rob Palmer <rob.palmer2@gmail.com>
1 parent c6df7d2 commit 95047b1

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

apps/site/pages/en/learn/typescript/run-natively.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Running TypeScript Natively
33
layout: learn
4-
authors: AugustinMauroy
4+
authors: AugustinMauroy, khaosdoctor, jakebailey, robpalme
55
---
66

77
> **⚠️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
1414

1515
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.
1616

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.
1818

1919
```bash
2020
node --experimental-strip-types example.ts
2121
```
2222

2323
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.
2424

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:
2626

2727
```bash
28-
node --experimental-strip-types --experimental-transform-types another-example.ts
28+
node --experimental-transform-types another-example.ts
29+
```
30+
31+
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+
enum MyEnum {
43+
A,
44+
B,
45+
}
46+
47+
console.log(MyEnum.A);
2948
```
3049

3150
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
3453

3554
At the time of writing, the experimental support for TypeScript in Node.js has some limitations.
3655

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**.
3863

3964
## Important notes
4065

0 commit comments

Comments
 (0)