You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/9/en/part9b.md
+23-20Lines changed: 23 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ Let's start writing our first TypeScript app. To keep things simple, let's start
25
25
You can install both <i>ts-node</i> and the official <i>typescript</i> package globally by running:
26
26
27
27
```bash
28
-
npm install -g ts-node typescript
28
+
npm install --location=global ts-node typescript
29
29
```
30
30
31
31
If you can't or don't want to install global packages, you can create an npm project which has the required dependencies and run your scripts in it.
@@ -61,7 +61,7 @@ It is worth mentioning that TypeScript also provides an online playground, where
61
61
62
62
#### A note about the coding style
63
63
64
-
JavaScript is a quite relaxed language in itself, and things can often be done in multiple different ways. For example, we have named vs anonymous functions, using const and let or var, and the use of <i>semicolons</i>. This part of the course differs from the rest by using semicolons. It is not a TypeScript-specific pattern but a general coding style decision taken when creating any kind of JavaScript project. Whether to use them or not is usually in the hands of the programmer, but since it is expected to adapt one's coding habits to the existing codebase, you are expected to use semicolons and adjust to the coding style in the exercises for this part. This part has some other coding style differences compared to the rest of the course as well, e.g. in the directory naming conventions.
64
+
JavaScript is a quite relaxed language in itself, and things can often be done in multiple different ways. For example, we have named vs anonymous functions, using const and let or var, and the optional use of <i>semicolons</i>. This part of the course differs from the rest by using semicolons. It is not a TypeScript-specific pattern but a general coding style decision taken when creating any kind of JavaScript project. Whether to use them or not is usually in the hands of the programmer, but since it is expected to adapt one's coding habits to the existing codebase, you are expected to use semicolons and adjust to the coding style in the exercises for this part. This part has some other coding style differences compared to the rest of the course as well, e.g. in the directory naming conventions.
65
65
66
66
Let us add a configuration file *tsconfig.json* to the project with the following content:
67
67
@@ -87,7 +87,7 @@ multiplicator(2, 4, 'Multiplied numbers 2 and 4, the result is:');
87
87
```
88
88
89
89
As you can see, this is still ordinary basic JavaScript with no additional TS features. It compiles and runs nicely with *npm run ts-node -- multiplier.ts*, as it would with Node.
90
-
90
+
91
91
But what happens if we end up passing the wrong <i>types</i> of arguments to the multiplicator function?
92
92
93
93
Let's try it out!
@@ -108,7 +108,7 @@ This is where we see the first benefits of TypeScript. Let's add types to the p
108
108
109
109
TypeScript natively supports multiple types including *number*, *string* and *Array*. See the comprehensive list [here](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html). More complex custom types can also be created.
110
110
111
-
The first two parameters of our function are the number and the string[primitives](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#the-primitives-string-number-and-boolean), respectively:
111
+
The first two parameters of our function are of type number and the last one is of type string, both types are [primitives](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#the-primitives-string-number-and-boolean):
The compiler complains straight away because, in one case, the function returns a string. There are a couple of ways to fix this. We could extend the return type to allow string values, like so:
191
+
The compiler complains straight away because, in one case, the function returns a string. There are a couple of ways to fix this:
192
+
193
+
We could extend the return type to allow string values, like so:
192
194
193
195
```js
194
-
constcalculator= (a: number, b: number, op: Operation): number |string=> {
196
+
constcalculator= (a: number, b: number, op: Operation): number |string=> {
195
197
// ...
196
198
}
197
199
```
@@ -285,7 +287,7 @@ Here the narrowing was done with the [instanceof](https://www.typescriptlang.org
285
287
286
288
The programs we have written are alright, but it sure would be better if we could use command-line arguments instead of always having to change the code to calculate different things.
287
289
288
-
Let's try it out, as we would in a regular Node application, by accessing *process.argv*. If you are using a recent npm-version (7.0 or later), there are no problems but with an older setup something is not right:
290
+
Let's try it out, as we would in a regular Node application, by accessing *process.argv*. If you are using a recent npm-version (7.0 or later), there are no problems, but with an older setup something is not right:
289
291
290
292

291
293
@@ -297,7 +299,7 @@ Let's return to the basic idea of TypeScript. TypeScript expects all globally-us
297
299
298
300
As with npm, the TypeScript world also celebrates open-source code. The community is active and continuously reacting to updates and changes in commonly-used npm packages. You can almost always find the typings for npm packages, so you don't have to create types for all of your thousands of dependencies alone.
299
301
300
-
Usually, types for existing packages can be found from the <i>@types</i> organization within npm, and you can add the relevant types to your project by installing an npm package with the name of your package with a @types/ prefix. For example:
302
+
Usually, types for existing packages can be found from the <i>@types</i> organization within npm, and you can add the relevant types to your project by installing an npm package with the name of your package with a *@types/* prefix. For example:
@@ -373,8 +375,7 @@ it "works" but gives us the answer:
373
375
Multiplied 5 and NaN, the result is: NaN
374
376
```
375
377
376
-
The reason for this is, that *Number('lol')* returns *NaN*,
377
-
which is actually of type *number*, so TypeScript has no power to rescue us from this kind of situation.
378
+
The reason for this is, that *Number('lol')* returns *NaN*, which is actually of type *number*, so TypeScript has no power to rescue us from this kind of situation.
378
379
379
380
To prevent this kind of behavior, we have to validate the data given to us from the command line.
380
381
@@ -456,16 +457,16 @@ The definition utilizes TypeScript's [Interface](https://www.typescriptlang.org/
456
457
Note that there is also an alternative syntax for [arrays](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#arrays) in TypeScript. Instead of writing
457
458
458
459
```js
459
-
let values: number[];
460
+
let values: number[];
460
461
```
461
462
462
463
we could use the "generics syntax" and write
463
464
464
465
```js
465
-
let values:Array<number>;
466
+
let values:Array<number>;
466
467
```
467
468
468
-
In this course we shall mostly be following the convention enforced by the Eslint rule [array-simple](https://typescript-eslint.io/rules/array-type/#array-simple) that suggests to write the simple arrays with the [] syntax and use the the <> syntax for the more complex ones, see [here](https://typescript-eslint.io/rules/array-type/#array-simple) for examples.
469
+
In this course we shall mostly be following the convention enforced by the Eslint rule [array-simple](https://typescript-eslint.io/rules/array-type/#array-simple) that suggests to write the simple arrays with the [] syntax and use the <> syntax for the more complex ones.
469
470
470
471
</div>
471
472
@@ -538,7 +539,8 @@ If you call the function with parameters *[3, 0, 2, 4.5, 0, 3, 1]* and *2*, it s
538
539
rating:2,
539
540
ratingDescription:'not too bad but could be better',
540
541
target:2,
541
-
average:1.9285714285714286 }
542
+
average:1.9285714285714286
543
+
}
542
544
```
543
545
544
546
Create an npm script, *npm run calculateExercises*, to call the function with hard-coded values.
ratingDescription: 'not too bad but could be better',
568
570
target: 2,
569
-
average: 1.7222222222222223 }
571
+
average: 1.7222222222222223
572
+
}
570
573
```
571
574
572
575
In the example, the <i>first argument</i> is the target value.
@@ -592,7 +595,7 @@ default export "this is the default..."
592
595
593
596
Another note: somehow surprisingly TypeScript does not allow to define the same variable in many files at a "block-scope", that is, outside functions (or classes):
594
597
595
-

This is actually not quite true. This rule applies only to files that are treated as "scripts". A file is a script if it does not contain any export or import statements. If a file has those, then the file is treated as a [module](https://www.typescriptlang.org/docs/handbook/modules.html), <i>and</i> the variables do not get defined in the block-scope.
598
601
@@ -654,7 +657,7 @@ and then add the *start* script to package.json:
654
657
}
655
658
```
656
659
657
-
Now we can create the file <i>index.ts</i>, and write the HTTP GET *ping</i> endpoint to it:
660
+
Now we can create the file <i>index.ts</i>, and write the HTTP GET *ping* endpoint to it:
And no more errors! Let's take a look at what changed.
699
702
700
-
When we hover over the *require* statement, we can see the compiler interprets everything express-related to be of type*any*.
703
+
When we hover over the *require* statement, we can see that the compiler interprets everything express-related to be of type *any*.
701
704
702
705

703
706
@@ -833,7 +836,7 @@ Do not copy the calculator code to file <i>index.ts</i>; instead, make it a [Typ
833
836
834
837
### The horrors of *any*
835
838
836
-
Now that we have our first endpoints completed, you might notice we have used barely any TypeScript in these small examples. When examining the code a bit closer, we can see a few dangers lurking there.
839
+
Now that we have our first endpoints completed, you might notice that we have used barely any TypeScript in these small examples. When examining the code a bit closer, we can see a few dangers lurking there.
837
840
838
841
Let's add the HTTP POST endpoint *calculate* to our app:
0 commit comments