Skip to content

Commit 7e81962

Browse files
committed
part9b
1 parent c9d607d commit 7e81962

File tree

2 files changed

+185
-65
lines changed

2 files changed

+185
-65
lines changed

src/content/9/en/part9b.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -864,15 +864,15 @@ export const calculator = (a: number, b: number, op: Operation) : number => {
864864
865865
When you hover over the *calculate* function, you can see the typing of the *calculator* even though the code itself does not contain any typings:
866866
867-
![vscode showing calculator types when mouse over function](../../images/9/12a21.png)
867+
![vscode showing calculator types when hovering function](../../images/9/12a21.png)
868868
869869
But if you hover over the values parsed from the request, an issue arises:
870870
871871
![vscode problematically showing any when hovering over values parsed in to calculate](../../images/9/13a21.png)
872872
873873
All of the variables have the type *any*. It is not all that surprising, as no one has given them a type yet. There are a couple of ways to fix this, but first, we have to consider why this is accepted and where the type *any* came from.
874874
875-
In TypeScript, every untyped variable whose type cannot be inferred implicitly becomes type [any](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any). Any is a kind of "wild card" type which stands for <i>whatever</i> type.
875+
In TypeScript, every untyped variable whose type cannot be inferred implicitly becomes of type [any](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any). Any is a kind of "wild card" type, which stands for <i>whatever</i> type.
876876
Things become implicitly any type quite often when one forgets to type functions.
877877
878878
We can also explicitly type things *any*. The only difference between the implicit and explicit any type is how the code looks; the compiler does not care about the difference.
@@ -888,15 +888,14 @@ const a : any = /* no clue what the type will be! */.
888888
889889
We already have <i>noImplicitAny: true</i> configured in our example, so why does the compiler not complain about the implicit *any* types? The reason is that the *body* field of an Express [Request](https://expressjs.com/en/5x/api.html#req) object is explicitly typed *any*. The same is true for the *request.query* field that Express uses for the query parameters.
890890
891-
What if we would like to restrict developers from using the *any* type? Fortunately, we have methods other than <i>tsconfig.json</i> to enforce a coding style. What we can do is use <i>ESlint</i> to manage
892-
our code.
891+
What if we would like to restrict developers from using the *any* type? Fortunately, we have methods other than <i>tsconfig.json</i> to enforce a coding style. What we can do is use <i>ESlint</i> to manage our code.
893892
Let's install ESlint and its TypeScript extensions:
894893
895894
```shell
896895
npm install --save-dev eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser
897896
```
898897
899-
We will configure ESlint to [disallow explicit any]( https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-explicit-any.md). Write the following rules to <i>.eslintrc</i>:
898+
We will configure ESlint to [disallow explicit any]( https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/no-explicit-any.md). Write the following rules to <i>.eslintrc</i>:
900899
901900
```json
902901
{
@@ -982,7 +981,7 @@ Disabling *@typescript-eslint/no-unsafe-assignment* for the destructuring assign
982981
```js
983982
app.post('/calculate', (req, res) => {
984983
// highlight-start
985-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
984+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
986985
// highlight-end
987986
const { value1, value2, op } = req.body;
988987

@@ -1031,11 +1030,11 @@ app.post('/calculate', (req, res) => {
10311030
});
10321031
```
10331032
1034-
We shall see later on in this part some techniques how the <i>any</i> typed data (eg. the input an app receives from the user) can be <i>narrowed</i> to a more specific type (such as number). With a proper narrowing of types, there is no more need to silence the eslint rules.
1033+
We shall see later in this part some techniques on how the *any* typed data (eg. the input an app receives from the user) can be *narrowed* to a more specific type (such as number). With a proper narrowing of types, there is no more need to silence the ESlint rules.
10351034
10361035
### Type assertion
10371036
1038-
Using a [type assertion](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions) is another "dirty trick" that can be done to keep TypeScript compiler and Eslint quiet. Let us export the type Operation in <i>calculator.ts</i>:
1037+
Using a [type assertion](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions) is another "dirty trick" that can be done to keep TypeScript compiler and Eslint quiet. Let us export the type Operation in *calculator.ts*:
10391038
10401039
```js
10411040
export type Operation = 'multiply' | 'add' | 'divide';
@@ -1072,13 +1071,13 @@ app.post('/calculate', (req, res) => {
10721071

10731072
const result = calculator(
10741073
Number(value1), Number(value2), op as Operation // highlight-line
1075-
);
1074+
);
10761075

10771076
return res.send({ result });
10781077
});
10791078
```
10801079
1081-
Using a type assertion (or quieting an Eslint rule) is always a bit risky thing. It leaves the TypeScript compiler off the hook, the compiler just trusts that we as developers know what we are doing. If the asserted type does <i>not</i> have the right kind of value, the result will be a runtime error, so one must be pretty careful when validating the data if a type assertion is used.
1080+
Using a type assertion (or quieting an Eslint rule) is always a bit risky thing. It leaves the TypeScript compiler off the hook, the compiler just trusts that we as developers know what we are doing. If the asserted type does *not* have the right kind of value, the result will be a runtime error, so one must be pretty careful when validating the data if a type assertion is used.
10821081
10831082
In the next chapter, we shall have a look at [type narrowing](https://www.typescriptlang.org/docs/handbook/2/narrowing.html) which will provide a much more safe way of giving a stricter type for data that is coming from an external source.
10841083
@@ -1094,7 +1093,7 @@ Configure your project to use the above ESlint settings and fix all the warnings
10941093
10951094
#### 9.7 WebExercises
10961095
1097-
Add an endpoint to your app for the exercise calculator. It should be used by doing an HTTP POST request to the endpoint <http://localhost:3002/exercises> with the input in the request body:
1096+
Add an endpoint to your app for the exercise calculator. It should be used by doing a HTTP POST request to the endpoint <http://localhost:3002/exercises> with the following input in the request body:
10981097
10991098
```js
11001099
{

0 commit comments

Comments
 (0)