Skip to content

Commit 878d2ee

Browse files
authored
Merge pull request #3907 from leburgeon/patch-10
Update part9d.md
2 parents fc23240 + 8e50148 commit 878d2ee

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/content/9/en/part9d.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ When we hover over the *response.data* we see that it has the type *any*
821821
822822
To set the data to the state with function *setNotes* we must type it properly.
823823
824-
With a little [help from internet](https://upmostly.com/typescript/how-to-use-axios-in-your-typescript-apps), we find a clever trick:
824+
With a little [help from the internet](https://upmostly.com/typescript/how-to-use-axios-in-your-typescript-apps), we find a clever trick:
825825
826826
```js
827827
useEffect(() => {
@@ -845,11 +845,11 @@ We can now set the data in the state *notes* to get the code working:
845845
}, [])
846846
```
847847
848-
So just like with *useState*, we gave a type parameter to *axios.get* to instruct it on how the typing should be done. Just like *useState* also *axios.get* is a [generic function](https://www.typescriptlang.org/docs/handbook/2/generics.html#working-with-generic-type-variables). Unlike some generic functions, the type parameter of *axios.get* has a default value of *any* so, if the function is used without defining the type parameter, the type of the response data will be any.
848+
So just like with *useState*, we gave a type parameter to *axios.get* to instruct it on how the typing should be done. Just like *useState*, *axios.get* is also a [generic function](https://www.typescriptlang.org/docs/handbook/2/generics.html#working-with-generic-type-variables). Unlike some generic functions, the type parameter of *axios.get* has a default value of *any* so, if the function is used without defining the type parameter, the type of the response data will be any.
849849
850-
The code works, compiler and Eslint are happy and remain quiet. However, giving a type parameter to *axios.get* is a potentially dangerous thing to do. The <i>response body can contain data in an arbitrary form</i>, and when giving a type parameter we are essentially just telling to TypeScript compiler to trust us that the data has type *Note[]*.
850+
The code works, compiler and Eslint are happy and remain quiet. However, giving a type parameter to *axios.get* is a potentially dangerous thing to do. The <i>response body can contain data in an arbitrary form</i>, and when giving a type parameter we are essentially just telling the TypeScript compiler to trust us that the data has type *Note[]*.
851851
852-
So our code is essentially as safe as it would be if a [type assertion](/en/part9/first_steps_with_type_script#type-assertion) would be used:
852+
So our code is essentially as safe as it would be if a [type assertion](/en/part9/first_steps_with_type_script#type-assertion) would be used (not good):
853853
854854
```js
855855
useEffect(() => {
@@ -862,7 +862,7 @@ So our code is essentially as safe as it would be if a [type assertion](/en/part
862862
863863
Since the TypeScript types do not even exist in runtime, our code does not give us any safety against situations where the request body contains data in the wrong form.
864864
865-
Giving a type parameter to *axios.get* might be ok if we are <i>absolutely sure</i> that the backend behaves correctly and returns always the data in the correct form. If we want to build a robust system we should prepare for surprises and parse the response data in the frontend, similarly to what we did [in the previous section](/en/part9/typing_an_express_app#proofing-requests) for the requests to the backend.
865+
Giving a type parameter to *axios.get* might be ok if we are <i>absolutely sure</i> that the backend behaves correctly and always returns the data in the correct form. If we want to build a robust system we should prepare for surprises and parse the response data (similar to what we did [in the previous section](/en/part9/typing_an_express_app#proofing-requests) for the requests to the backend).
866866
867867
Let us now wrap up our app by implementing the new note addition:
868868
@@ -979,7 +979,7 @@ interface CoursePartBase {
979979
}
980980
```
981981
982-
We actually could have had the same effect by using a [type alias](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-aliases)
982+
We actually could have achieved the same effect by using a [type alias](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-aliases)
983983
984984
```js
985985
type DiaryEntry = {

0 commit comments

Comments
 (0)