Skip to content

Commit 52a3ea8

Browse files
committed
save progress
1 parent e2eb165 commit 52a3ea8

File tree

2 files changed

+318
-184
lines changed

2 files changed

+318
-184
lines changed

src/content/9/en/part9b.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Let's start writing our first TypeScript app. To keep things simple, let's start
2525
You can install both <i>ts-node</i> and the official <i>typescript</i> package globally by running:
2626

2727
```bash
28-
npm install -g ts-node typescript
28+
npm install --location=global ts-node typescript
2929
```
3030

3131
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
6161

6262
#### A note about the coding style
6363

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

6666
Let us add a configuration file *tsconfig.json* to the project with the following content:
6767

@@ -87,7 +87,7 @@ multiplicator(2, 4, 'Multiplied numbers 2 and 4, the result is:');
8787
```
8888

8989
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+
9191
But what happens if we end up passing the wrong <i>types</i> of arguments to the multiplicator function?
9292

9393
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
108108

109109
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.
110110

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):
112112

113113
```js
114114
const multiplicator = (a: number, b: number, printText: string) => {
@@ -123,7 +123,7 @@ Now the code is no longer valid JavaScript but in fact TypeScript. When we try t
123123
![terminal output showing error assigning string to number](../../images/9/2a.png)
124124

125125
One of the best things about TypeScript's editor support is that you don't necessarily need to even run the code to see the issues.
126-
The VSCode plugin is so efficient, that it informs you immediately when you are trying to use an incorrect type:
126+
VSCode is so efficient, that it informs you immediately when you are trying to use an incorrect type:
127127

128128
![vscode showing same error about string as number](../../images/9/2.png)
129129

@@ -188,10 +188,12 @@ const calculator = (a: number, b: number, op: Operation): number => { // highlig
188188
}
189189
```
190190

191-
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:
192194

193195
```js
194-
const calculator = (a: number, b: number, op: Operation): number | string => {
196+
const calculator = (a: number, b: number, op: Operation): number | string => {
195197
// ...
196198
}
197199
```
@@ -285,7 +287,7 @@ Here the narrowing was done with the [instanceof](https://www.typescriptlang.org
285287

286288
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.
287289

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

290292
![vs code error cannot find name process need to install type definitions](../../images/9/5.png)
291293

@@ -297,7 +299,7 @@ Let's return to the basic idea of TypeScript. TypeScript expects all globally-us
297299

298300
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.
299301

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

302304
```bash
303305
npm install --save-dev @types/react @types/express @types/lodash @types/jest @types/mongoose
@@ -373,8 +375,7 @@ it "works" but gives us the answer:
373375
Multiplied 5 and NaN, the result is: NaN
374376
```
375377

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

379380
To prevent this kind of behavior, we have to validate the data given to us from the command line.
380381

@@ -456,16 +457,16 @@ The definition utilizes TypeScript's [Interface](https://www.typescriptlang.org/
456457
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
457458

458459
```js
459-
let values: number[];
460+
let values: number[];
460461
```
461462

462463
we could use the "generics syntax" and write
463464

464465
```js
465-
let values: Array<number>;
466+
let values: Array<number>;
466467
```
467468

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

470471
</div>
471472

@@ -538,7 +539,8 @@ If you call the function with parameters *[3, 0, 2, 4.5, 0, 3, 1]* and *2*, it s
538539
rating: 2,
539540
ratingDescription: 'not too bad but could be better',
540541
target: 2,
541-
average: 1.9285714285714286 }
542+
average: 1.9285714285714286
543+
}
542544
```
543545

544546
Create an npm script, *npm run calculateExercises*, to call the function with hard-coded values.
@@ -566,7 +568,8 @@ $ npm run calculateExercises 2 1 0 2 4.5 0 3 1 0 4
566568
rating: 2,
567569
ratingDescription: 'not too bad but could be better',
568570
target: 2,
569-
average: 1.7222222222222223 }
571+
average: 1.7222222222222223
572+
}
570573
```
571574

572575
In the example, the <i>first argument</i> is the target value.
@@ -592,7 +595,7 @@ default export "this is the default..."
592595

593596
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):
594597

595-
![browser showing pong from localhost:3000/ping](../../images/9/60new.png)
598+
![vs code showing error cannot redeclare block-scoped variable x](../../images/9/60new.png)
596599

597600
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.
598601

@@ -654,7 +657,7 @@ and then add the *start* script to package.json:
654657
}
655658
```
656659

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

659662
```js
660663
const express = require('express');
@@ -697,7 +700,7 @@ npm install --save-dev @types/express
697700

698701
And no more errors! Let's take a look at what changed.
699702

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*.
701704

702705
![vscode showing problem of implicitly having any type](../../images/9/8a.png)
703706

@@ -833,7 +836,7 @@ Do not copy the calculator code to file <i>index.ts</i>; instead, make it a [Typ
833836

834837
### The horrors of *any*
835838

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

838841
Let's add the HTTP POST endpoint *calculate* to our app:
839842

0 commit comments

Comments
 (0)