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: docs/documentation/fr/handbook-v2/Narrowing.md
+25-27Lines changed: 25 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -122,27 +122,27 @@ function getUsersOnlineMessage(numUsersOnline: number) {
122
122
}
123
123
```
124
124
125
-
En JavaScript, constructs like `if`first "coerce" their conditions to `boolean`s to make sense of them, and then choose their branches depending on whether the result is `true`or`false`.
126
-
Values like
125
+
En JavaScript, les expressions tel que `if`convertissent leurs conditions vers des `boolean`s pour les comprendre, puis choisissent leur branche selon si le résultat est `true`ou`false`.
126
+
Les valeurs
127
127
128
128
-`0`
129
129
-`NaN`
130
-
-`""` (the empty string)
131
-
-`0n` (the `bigint` version of zero)
130
+
-`""` (chaîne de caractères vide)
131
+
-`0n` (zéro mais en `bigint`)
132
132
-`null`
133
133
-`undefined`
134
134
135
-
all coerce to `false`, and other values get coerced`true`.
136
-
You can always coerce values to `boolean`s by running them through the `Boolean` function, or by using the shorter double-Boolean negation. (The latter has the advantage that TypeScript infers a narrow literal boolean type `true`, while inferring the first as type `boolean`.)
135
+
sont toutes converties en `false`, et les autres valeurs sont converties en`true`.
136
+
Vous pouvez obtenir des `boolean`s en passant ces valeurs à la fonction `Boolean`, ou grâce à la double négation (TypeScript pourra inférer `true` dans le deuxième cas, tandis qu'il se contentera de `boolean` dans le premier.)
137
137
138
138
```ts twoslash
139
-
//both of these result in 'true'
139
+
//les deux renverront 'true'
140
140
Boolean("hello"); // type: boolean, value: true
141
141
!!"world"; // type: true, value: true
142
142
```
143
143
144
-
It's fairly popular to leverage this behavior, especially for guarding against values like `null`or`undefined`.
145
-
As an example, let's try using it for our `printAll` function.
144
+
Ce comportement est utilisé assez souvent, surtout pour se prémunir de valeurs tel que `null`ou`undefined`.
145
+
Essayons de l'appliquer à notre fonction `printAll`.
We wrapped the entire body of the function in a truthy check, but this has a subtle downside: we may no longer be handling the empty string case correctly.
186
+
Nous avons entouré tout le corps de la fonction dans une vérification de valeur _truthy_, mais cette approche possède un défaut subtil : on ne gère plus le cas de la chaîne de caractères vide correctement. Les développeurs moins expérimentés avec JavaScript pourraient tomber dans ce piège.
188
187
189
-
TypeScript doesn't hurt us here at all, but this is behavior worth noting if you're less familiar with JavaScript.
190
-
TypeScript can often help you catch bugs early on, but if you choose to do _nothing_ with a value, there's only so much that it can do without being overly prescriptive.
191
-
If you want, you can make sure you handle situations like these with a linter.
188
+
TypeScript est souvent capable de repérer les bugs existants, mais si vous décidez de ne _rien_ faire avec une valeur, il ne peut pas non plus pallier à vos oublis sans être trop strict.
189
+
Un linter, cependant, peut signaler ces cas de figure.
192
190
193
-
One last word on narrowing by truthiness is that Boolean negations with `!` filter out from negated branches.
191
+
Une dernière remarque sur le filtrage par `Boolean`, c'est que le système de typage s'adaptera dans la branche appropriée et omettra les types `falsy` (qui ne sont, donc, pas `truthy`) de ses prédictions.
194
192
195
193
```ts twoslash
196
194
function multiplyAll(
@@ -205,15 +203,15 @@ function multiplyAll(
205
203
}
206
204
```
207
205
208
-
## Equality narrowing
206
+
## Rétrécissement par égalités
209
207
210
-
TypeScript also uses `switch`statements and equality checks like `===`, `!==`, `==`, and`!=`to narrow types.
211
-
For example:
208
+
TypeScript utilise également les déclarations `switch`ainsi que les vérifications d'égalités, comme `===`, `!==`, `==`, et`!=`pour rétrécir les types.
209
+
Par exemple :
212
210
213
211
```ts twoslash
214
212
function example(x:string|number, y:string|boolean) {
215
213
if (x===y) {
216
-
//We can now call any 'string' method on 'x' or 'y'.
214
+
//On peut appeler toutes les méthodes de 'string' sur x ou y.
When we checked that `x`and`y`are both equal in the above example, TypeScript knew their types also had to be equal.
231
-
Since `string`is the only common type that both `x`and`y`could take on, TypeScript knows that`x`and`y`must be a`string`in the first branch.
228
+
Quand on a vérifié que `x`et`y`sont égaux, TypeScript a su que leurs types devaient aussi être égaux.
229
+
Vu que `string`est le seul type que `x`et`y`peuvent avoir, TypeScript sait que`x`et`y`doivent être des`string`dans la première branche.
232
230
233
-
Checking against specific literal values (as opposed to variables) works also.
231
+
Accomplir des vérifications contre des valeurs (et non pas des variables) fonctionne également.
234
232
In our section about truthiness narrowing, we wrote a `printAll` function which was error-prone because it accidentally didn't handle empty strings properly.
235
233
Instead we could have done a specific check to block out `null`s, and TypeScript still correctly removes `null` from the type of `strs`.
0 commit comments