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
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:
@@ -835,15 +843,13 @@ We can also explicitly type things *any*. The only difference between the implic
835
843
Programmers however see the code differently when *any* is explicitly enforced than when it is implicitly inferred.
836
844
Implicit *any* typings are usually considered problematic, since it is quite often due to the coder forgetting to assign types (or being too lazy to do it), and it also means that the full power of TypeScript is not properly exploited.
837
845
838
-
This is why the configuration rule [noImplicitAny](https://www.typescriptlang.org/tsconfig#noImplicitAny) exists on the compiler level, and it is highly recommended to keep it on at all times.
839
-
In the rare occasions when you truly cannot know what the type of a variable is, you should explicitly state that in the code:
846
+
This is why the configuration rule [noImplicitAny](https://www.typescriptlang.org/tsconfig#noImplicitAny) exists on the compiler level, and it is highly recommended to keep it on at all times. In the rare occasions when you truly cannot know what the type of a variable is, you should explicitly state that in the code:
840
847
841
848
```js
842
849
const a : any = /* no clue what the type will be!*/.
843
850
```
844
851
845
-
We already have <i>noImplicitAny</i> configured in our example, so why does the compiler not complain about the implicit *any* types?
846
-
The reason is that the *query* field of an express [Request](https://expressjs.com/en/5x/api.html#req) object is explicitly typed *any*. The same is truefor the *request.body* field we use to post data to an app.
852
+
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 truefor the *request.query* field that Express uses for the query parameters.
847
853
848
854
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
849
855
our code.
@@ -934,7 +940,7 @@ Quite a few semicolons are missing, but those are easy to add. We also have to s
934
940
935
941
We could and probably should disable some ESlint rules to get the data from the request body.
936
942
937
-
Disabling *@typescript-eslint/no-unsafe-assignment*for the destructuring assignment is nearly enough:
943
+
Disabling *@typescript-eslint/no-unsafe-assignment*for the destructuring assignment and calling the [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number) constructor to values is nearly enough:
const result = calculator(Number(value1), Number(value2), op);
987
-
return res.send(result);
993
+
return res.send({ result });
988
994
});
989
995
```
990
996
997
+
We shall see later on in this parts some techniques how the <i>any</i> typed data (eg. the input an app recieves 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.
998
+
999
+
### Type assertion
1000
+
1001
+
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>calcultor.ts</i>:
1002
+
1003
+
```js
1004
+
exporttype Operation = 'multiply'|'add'|'divide';
1005
+
```
1006
+
1007
+
Now we can import the type and use a <i>type assertion</i> to tell the TypeScript compiler what type a variable has:
1008
+
1009
+
```js
1010
+
import { calculator, Operation } from './calculator'; // highligh-line
const operation = op as Operation; // highlight-line
1020
+
1021
+
const result = calculator(Number(value1), Number(value2), operation); // highlight-line
1022
+
1023
+
return res.send({ result });
1024
+
});
1025
+
```
1026
+
1027
+
The defined constant _operation_ has now the type _Operation_ and the compiler is perfectly happy, no quieting of the Eslint rule is needed on the following functioncall. The new variable is actually not needed, the type assertion can be done when an argument is passed to the function:
Number(value1), Number(value2), op as Operation // highlight-line
1038
+
);
1039
+
1040
+
return res.send({ result });
1041
+
});
1042
+
```
1043
+
1044
+
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.
1045
+
1046
+
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 typefor data that is coming from an external source.
0 commit comments