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

content(learn): update resources about typescript #6951

Merged
merged 10 commits into from
Sep 16, 2024
10 changes: 9 additions & 1 deletion apps/site/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@
"anIntroductionToTheNpmPackageManager": "An introduction to the npm package manager",
"ecmascript2015Es6AndBeyond": "ECMAScript 2015 (ES6) and beyond",
"nodejsTheDifferenceBetweenDevelopmentAndProduction": "Node.js, the difference between development and production",
"nodejsWithTypescript": "Node.js with TypeScript",
"nodejsWithWebassembly": "Node.js with WebAssembly",
"debugging": "Debugging Node.js",
"profiling": "Profiling Node.js Applications",
"securityBestPractices": "Security Best Practices"
}
},
"typescript": {
"links": {
"typescript": "TypeScript",
"introduction": "Introduction to TypeScript",
"transpile": "Running TypeScript code using transpilation",
"run": "Running TypeScript with a runner",
"runNatively": "Running TypeScript with a Node.js Itself"
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved
}
},
"asynchronousWork": {
"links": {
"asynchronousWork": "Asynchronous Work",
Expand Down
25 changes: 21 additions & 4 deletions apps/site/navigation.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,6 @@
"link": "/learn/getting-started/nodejs-the-difference-between-development-and-production",
"label": "components.navigation.learn.gettingStarted.links.nodejsTheDifferenceBetweenDevelopmentAndProduction"
},
"nodejsWithTypescript": {
"link": "/learn/getting-started/nodejs-with-typescript",
"label": "components.navigation.learn.gettingStarted.links.nodejsWithTypescript"
},
"nodejsWithWebassembly": {
"link": "/learn/getting-started/nodejs-with-webassembly",
"label": "components.navigation.learn.gettingStarted.links.nodejsWithWebassembly"
Expand All @@ -185,6 +181,27 @@
}
}
},
"typescript": {
"label": "components.navigation.learn.typescript.links.typescript",
"items": {
"introduction": {
"link": "/learn/typescript/introduction",
"label": "components.navigation.learn.typescript.links.introduction"
},
"transpile": {
"link": "/learn/typescript/transpile",
"label": "components.navigation.learn.typescript.links.transpile"
},
"run": {
"link": "/learn/typescript/run",
"label": "components.navigation.learn.typescript.links.run"
},
"runNatively": {
"link": "/learn/typescript/run-natively",
"label": "components.navigation.learn.typescript.links.runNatively"
}
}
},
"asynchronousWork": {
"label": "components.navigation.learn.asynchronousWork.links.asynchronousWork",
"items": {
Expand Down
185 changes: 0 additions & 185 deletions apps/site/pages/en/learn/getting-started/nodejs-with-typescript.md

This file was deleted.

50 changes: 50 additions & 0 deletions apps/site/pages/en/learn/typescript/introduction.md
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. It's loved and used by a lot of software developers around the world.
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

Basically, it's a superset of JavaScript that adds new capabilities to the language. The most notable addition is static type definitions, something that is not present in plain JavaScript. Thanks to types, it's possible, for example, to declare what kind of arguments we are expecting and what is returned exactly in our functions or what's the exact shape of the object that we are creating. TypeScript is a really powerful tool and opens a new world of possibilities in JavaScript projects. It makes our code more secure and robust by preventing many bugs before the code is even shipped - it catches problems during code development and integrates wonderfully with code editors like Visual Studio Code.
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

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 would not comply with declared types, TypeScript would alarm us that something is wrong and prevent misuse. Secondly, not everything must be typed explicitly - TypeScript is very smart and can infer types for us. For example, variable `isJustineAnAdult` is of type `boolean` even if we didn't type it explicitly or `justine` would be valid argument for our function even though we didn't declare this variable as of `User` type.
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

## 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.
38 changes: 38 additions & 0 deletions apps/site/pages/en/learn/typescript/run-natively.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: Running TypeScript with a Node.js Itself
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved
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 on future versions of Node.js.
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

# Running TypeScript with a Node.js Itself
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

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 typed JavaScript code with Node.js?
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

```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 typed JavaScript code directly in Node.js without the need to transpile it first, and use TypeScript to catch type-related errors.
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved
In the future we all hope that this feature will be stable and available in the LTS version of Node.js, so that we can all enjoy it without any additional steps.
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

## 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.
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

You can get more information on the [api docs](https://nodejs.org/docs/latest/api/typescript.html#unsupported-typescript-features)
AugustinMauroy 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.
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

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.
49 changes: 49 additions & 0 deletions apps/site/pages/en/learn/typescript/run.md
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. But it's not typechecking your code. So we recommend to type check your code first with `tsc` and then run it with `ts-node` before shipping it.
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

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
```
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

## 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. But it's not typechecking your code. So we recommend to type check your code first with `tsc` and then run it with `tsx` before shipping it.
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

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
```
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

### Registering `tsx` via `node`

AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved
If you want to use `tsx` via `node`, you can register `tsx` via `--import`:

```bash
node --import=tsx example.ts
```
Loading
Loading