-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
content(learn): update resources about typescript (#6951)
* content(learn): update ressources about typescript * adding important note * content(learn): update from feedback Co-Authored-By: Antoine du Hamel <[email protected]> * content(learn): remove global install of tsc * fix fails from github copilot fix fails from github copilot * fix: remove old stuff from navigation * update * update * Update from feedback Co-Authored-By: Brian Muenzenmeyer <[email protected]> --------- Co-authored-by: Antoine du Hamel <[email protected]> Co-authored-by: Brian Muenzenmeyer <[email protected]>
- Loading branch information
1 parent
ae06243
commit d164714
Showing
9 changed files
with
294 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 0 additions & 185 deletions
185
apps/site/pages/en/learn/getting-started/nodejs-with-typescript.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
title: Introduction to TypeScript | ||
layout: learn | ||
authors: sbielenica, ovflowd, vaishnav-mk, AugustinMauroy | ||
--- | ||
|
||
# Introduction to TypeScript | ||
|
||
## What is TypeScript | ||
|
||
**[TypeScript](https://www.typescriptlang.org)** is an open-source language maintained and developed by Microsoft. | ||
|
||
Basically, TypeScript adds additional syntax to JavaScript to support a tighter integration with your editor. Catch errors early in your editor or in your CI/CD pipeline, and write more maintainable code. | ||
|
||
We can talk about other TypeScript benefits later, let's see some examples now! | ||
|
||
## First TypeScript code | ||
|
||
Take a look at this code snippet and then we can unpack it together: | ||
|
||
<!-- | ||
Maintainers note: this code is duplicated in the next article, please keep them in sync | ||
--> | ||
|
||
```ts | ||
type User = { | ||
name: string; | ||
age: number; | ||
}; | ||
|
||
function isAdult(user: User): boolean { | ||
return user.age >= 18; | ||
} | ||
|
||
const justine = { | ||
name: 'Justine', | ||
age: 23, | ||
} satisfies User; | ||
|
||
const isJustineAnAdult = isAdult(justine); | ||
``` | ||
|
||
The first part (with the `type` keyword) is responsible for declaring our custom object type representing users. Later we utilize this newly created type to create function `isAdult` that accepts one argument of type `User` and returns `boolean`. After this, we create `justine`, our example data that can be used for calling the previously defined function. Finally, we create a new variable with information on whether `justine` is an adult. | ||
|
||
There are additional things about this example that you should know. Firstly, if we do not comply with the declared types, TypeScript will inform us that something is wrong and prevent misuse. Secondly, not everything must be typed explicitly—TypeScript infers types for us. For example, the variable `isJustineAnAdult` is of type `boolean` even if we didn't type it explicitly, and `justine` would be a valid argument for our function even though we didn't declare this variable as of `User` type. | ||
|
||
## How to run TypeScript code | ||
|
||
Okay, so we have some TypeScript code. Now how do we run it? | ||
There are few possible ways to run TypeScript code, we will cover all of them in the next articles. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
title: Running TypeScript Natively | ||
layout: learn | ||
authors: AugustinMauroy | ||
--- | ||
|
||
> **⚠️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. | ||
# Running TypeScript Natively | ||
|
||
In the previous articles, we learned how to run TypeScript code using transpilation and with a runner. In this article, we will learn how to run TypeScript code using Node.js itself. | ||
|
||
## Running TypeScript code with Node.js | ||
|
||
Since V22.6.0, Node.js has experimental support for some TypeScript syntax. You can write code that's valid TypeScript directly in Node.js without the need to transpile it first. | ||
|
||
So how do you run TypeScript code with Node.js? | ||
|
||
```bash | ||
node --experimental-strip-types example.ts | ||
``` | ||
|
||
The `--experimental-strip-types` flag tells Node.js to strip the type annotations from the TypeScript code before running it. | ||
|
||
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. | ||
Future versions of Node.js will include support for TypeScript without the need for a command line flag. | ||
|
||
## Limitations | ||
|
||
At the time of writing, the experimental support for TypeScript in Node.js has some limitations. To allow TypeScript to run in node.js, our collaborators have chosen to only strip types from the code. | ||
|
||
You can get more information on the [API docs](https://nodejs.org/docs/latest/api/typescript.html#unsupported-typescript-features) | ||
|
||
## 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. | ||
|
||
We can understand that this feature is experimental and has some limitations; if that doesn't suit your use-case, please use something else, or contribute a fix. Bug reports are also welcome, please keep in mind the project is run by volunteers, without warranty of any kind, so please be patient if you can't contribute the fix yourself. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
title: Running TypeScript with a runner | ||
layout: learn | ||
authors: AugustinMauroy | ||
--- | ||
|
||
# Running TypeScript with a runner | ||
|
||
In the previous article, we learned how to run TypeScript code using transpilation. In this article, we will learn how to run TypeScript code using a runner. | ||
|
||
## Running TypeScript code with `ts-node` | ||
|
||
[ts-node](https://typestrong.org/ts-node/) is a TypeScript execution environment for Node.js. It allows you to run TypeScript code directly in Node.js without the need to compile it first. Note, however, that it does not type check your code. So we recommend to type check your code first with `tsc` and then run it with `ts-node` before shipping it. | ||
|
||
To use `ts-node`, you need to install it first: | ||
|
||
```bash | ||
npm i -D ts-node | ||
``` | ||
|
||
Then you can run your TypeScript code like this: | ||
|
||
```bash | ||
npx ts-node example.ts | ||
``` | ||
|
||
## Running TypeScript code with `tsx` | ||
|
||
[tsx](https://tsx.is/) is another TypeScript execution environment for Node.js. It allows you to run TypeScript code directly in Node.js without the need to compile it first. Note, however, that it does not type check your code. So we recommend to type check your code first with `tsc` and then run it with `tsx` before shipping it. | ||
|
||
To use `tsx`, you need to install it first: | ||
|
||
```bash | ||
npm i -D tsx | ||
``` | ||
|
||
Then you can run your TypeScript code like this: | ||
|
||
```bash | ||
npx tsx example.ts | ||
``` | ||
|
||
### Registering `tsx` via `node` | ||
|
||
If you want to use `tsx` via `node`, you can register `tsx` via `--import`: | ||
|
||
```bash | ||
node --import=tsx example.ts | ||
``` |
Oops, something went wrong.