Skip to content

Commit 5351908

Browse files
AugustinMauroythisalihassanbmuenzenmeyermikeestoovflowd
authored
chore(learn): update typescript (#6615)
* chore(learn): update typescript * fix: ts error output * fix type Co-authored-by: Ali Hassan <[email protected]> Signed-off-by: Augustin Mauroy <[email protected]> * fix typo Co-authored-by: Ali Hassan <[email protected]> Signed-off-by: Augustin Mauroy <[email protected]> * grammar logic Co-authored-by: Brian Muenzenmeyer <[email protected]> Signed-off-by: Augustin Mauroy <[email protected]> * update Co-authored-by: Brian Muenzenmeyer <[email protected]> Signed-off-by: Augustin Mauroy <[email protected]> * update title Co-authored-by: Brian Muenzenmeyer <[email protected]> Signed-off-by: Augustin Mauroy <[email protected]> * Update pages/en/learn/getting-started/nodejs-with-typescript.md Co-authored-by: Brian Muenzenmeyer <[email protected]> Signed-off-by: Augustin Mauroy <[email protected]> * Update pages/en/learn/getting-started/nodejs-with-typescript.md Co-authored-by: Brian Muenzenmeyer <[email protected]> Signed-off-by: Augustin Mauroy <[email protected]> * Update pages/en/learn/getting-started/nodejs-with-typescript.md Co-authored-by: Brian Muenzenmeyer <[email protected]> Signed-off-by: Augustin Mauroy <[email protected]> * Update pages/en/learn/getting-started/nodejs-with-typescript.md Co-authored-by: Michael Esteban <[email protected]> Signed-off-by: Brian Muenzenmeyer <[email protected]> * remove conclusion --------- Signed-off-by: Augustin Mauroy <[email protected]> Signed-off-by: Brian Muenzenmeyer <[email protected]> Co-authored-by: Ali Hassan <[email protected]> Co-authored-by: Brian Muenzenmeyer <[email protected]> Co-authored-by: Michael Esteban <[email protected]> Co-authored-by: Claudio W <[email protected]>
1 parent 7d8d9be commit 5351908

File tree

1 file changed

+56
-9
lines changed

1 file changed

+56
-9
lines changed

pages/en/learn/getting-started/nodejs-with-typescript.md

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
title: Node.js with TypeScript
33
layout: learn
4-
authors: sbielenica, ovflowd, vaishnav-mk
4+
authors: sbielenica, ovflowd, vaishnav-mk, AugustinMauroy
55
---
66

77
# Node.js with TypeScript
88

99
## What is TypeScript
1010

11-
**[TypeScript](https://www.typescriptlang.org)** is a trendy open-source language maintained and developed by Microsoft. It's loved and used by a lot of software developers around the world.
11+
**[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.
1212

1313
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.
1414

@@ -85,14 +85,14 @@ const isJustineAnAdult: string = isAdult(justine, "I shouldn't be here!");
8585
**And this is what TypeScript has to say about this:**
8686

8787
```console
88-
example.ts:12:3 - error TS2322: Type 'string' is not assignable to type 'number'.
88+
example.ts:12:5 - error TS2322: Type 'string' is not assignable to type 'number'.
8989

90-
12 age: "Secret!",
91-
~~~
90+
12 age: 'Secret!',
91+
~~~
9292

93-
example.ts:3:3
94-
3 age: number;
95-
~~~
93+
example.ts:3:5
94+
3 age: number;
95+
~~~
9696
The expected type comes from property 'age' which is declared here on type 'User'
9797

9898
example.ts:15:7 - error TS2322: Type 'boolean' is not assignable to type 'string'.
@@ -106,7 +106,7 @@ example.ts:15:51 - error TS2554: Expected 1 arguments, but got 2.
106106
~~~~~~~~~~~~~~~~~~~~~~
107107

108108

109-
Found 3 errors.
109+
Found 3 errors in the same file, starting at: example.ts:12
110110
```
111111

112112
As you can see TypeScript successfully prevents us from shipping code that could work unexpectedly. That's wonderful!
@@ -117,6 +117,53 @@ TypeScript offers a whole lot of other great mechanisms like interfaces, classes
117117

118118
Some of the other benefits of TypeScript that are worth mentioning are that it can be adopted progressively, it helps making code more readable and understandable and it allows developers to use modern language features while shipping code for older Node.js versions.
119119

120+
## Running TypeScript Code in Node.js
121+
122+
Node.js cannot run TypeScript natively. You cannot call `node example.ts` from the command line directly. But there are three solutions to this problem:
123+
124+
### Compiling TypeScript to JavaScript
125+
126+
If you want to run TypeScript code in Node.js, you need to compile it to JavaScript first. You can do this using the TypeScript compiler `tsc` as shown earlier.
127+
128+
Here's a small example:
129+
130+
```bash
131+
npx tsc example.ts
132+
node example.js
133+
```
134+
135+
### Running TypeScript Code with `ts-node`
136+
137+
You can use [ts-node](https://typestrong.org/ts-node/) 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.
138+
139+
To use `ts-node`, you need to install it first:
140+
141+
```bash
142+
npm i -D ts-node
143+
```
144+
145+
Then you can run your TypeScript code like this:
146+
147+
```bash
148+
npx ts-node example.ts
149+
```
150+
151+
### Running TypeScript Code with a Node.js Loader
152+
153+
Since Node.js v19.0.0, you can use a [custom loader](https://nodejs.org/api/cli.html#--importmodule). Download a loader such as `ts-node` or `tsx` or `nodejs-loaders`
154+
155+
First you need to install the loader:
156+
157+
```bash
158+
npm i -D ts-node
159+
```
160+
161+
Then you can run your TypeScript code like this:
162+
163+
```bash
164+
node --loader=ts-node/esm example.ts
165+
```
166+
120167
## TypeScript in the Node.js world
121168

122169
TypeScript is well-established in the Node.js world and used by many companies, open-source projects, tools and frameworks.

0 commit comments

Comments
 (0)