Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: update typescript docs #7412

Merged
merged 7 commits into from
Jan 21, 2025
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions apps/site/pages/en/learn/typescript/run-natively.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Running TypeScript Natively
layout: learn
authors: AugustinMauroy
authors: AugustinMauroy,khaosdoctor,jakebailey
khaosdoctor marked this conversation as resolved.
Show resolved Hide resolved
---

> **⚠️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.
Expand All @@ -22,10 +22,29 @@ node --experimental-strip-types example.ts

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.

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

```bash
node --experimental-strip-types --experimental-transform-types another-example.ts
node --experimental-transform-types another-example.ts
```

From version V23 onwards, the `--experimental-strip-types` flag is enabled by default, enabling you to run any supported syntax, so running files like the one below with `node file.ts` is supported:
jakebailey marked this conversation as resolved.
Show resolved Hide resolved

```ts
function foo(bar: number): string {
return 'hello';
}
```

However, running any code that requires transformations, like the code below still needs the use of `--experimental-transform-types`:

```ts
enum MyEnum {
A,
B,
}

console.log(MyEnum.A);
```

Future versions of Node.js will include support for TypeScript without the need for a command line flag.
Expand All @@ -36,6 +55,10 @@ At the time of writing, the experimental support for TypeScript in Node.js has s

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

### Configuration

Node.js's TypeScript loader ([Amaro](https://github.com/nodejs/amaro)) does not check the `tsconfig.json` file when transforming TypeScript code. In order to configure TypeScript to recognize (and match) this behavior when type checking, we recommend setting the `compilerOptions` listed in [here](https://nodejs.org/api/typescript.html#type-stripping), as well as using TypeScript on version **5.7 or higher**.
khaosdoctor marked this conversation as resolved.
Show resolved Hide resolved
khaosdoctor marked this conversation as resolved.
Show resolved Hide resolved

## Important notes

Thanks to all the contributors who have made this feature possible. We hope that this feature will be stable and available in the LTS version of Node.js soon.
Expand Down
Loading