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
oneline: "Créer des types qui agissent comme des déclarations if dans le système de typage."
6
6
---
7
7
@@ -187,8 +187,8 @@ type Flatten<Type> = Type extends Array<infer Item> ? Item : Type;
187
187
Ici, le mot-clé `infer` introduit un nouveau type générique variable appelé `Item`, au lieu de préciser comment récupérer le type élément de `T` dans la branche vrai.
188
188
Cela nous libère de devoir penser à la façon de creuser et obtenir manuellement les types qui nous intéressent.
189
189
190
-
We can write some useful helper type aliases using the `infer` keyword.
191
-
For example, for simple cases, we can extract the return type out from function types:
190
+
Nous pouvons écrire des types utiles grâce au mot-clé `infer`.
191
+
Pour les cas simples, il est possible d'inférer le type de retour d'une fonction :
When inferring from a type with multiple call signatures (such as the type of an overloaded function), inferences are made from the _last_ signature (which, presumably, is the most permissive catch-all case). It is not possible to perform overload resolution based on a list of argument types.
208
+
Lorsqu'on infère depuis une fonction qui a plusieurs signatures, le résultat est l'inférence de la _dernière_ (et donc probablement la plus laxiste). Il n'est pas possible d'inférer une signature particulière en se basant sur une liste d'arguments de types.
209
209
210
210
```ts twoslash
211
211
declarefunction stringOrNum(x:string):number;
@@ -216,16 +216,16 @@ type T1 = ReturnType<typeof stringOrNum>;
216
216
// ^?
217
217
```
218
218
219
-
## Distributive Conditional Types
219
+
## Types Conditionnels Distributifs
220
220
221
-
When conditional types act on a generic type, they become _distributive_ when given a union type.
222
-
For example, take the following:
221
+
Quand les types conditionnels opèrent sur un type générique, ils deviennent _distributifs_ quand on utilise également un type union.
222
+
Par exemple :
223
223
224
224
```ts twoslash
225
225
typeToArray<Type> =Typeextendsany?Type[] :never;
226
226
```
227
227
228
-
If we plug a union type into `ToArray`, then the conditional type will be applied to each member of that union.
228
+
Si `ToArray` reçoit un type union, le type conditionnel va s'appliquer sur chaque membre de ce type union.
229
229
230
230
```ts twoslash
231
231
typeToArray<Type> =Typeextendsany?Type[] :never;
@@ -234,15 +234,15 @@ type StrArrOrNumArr = ToArray<string | number>;
234
234
// ^?
235
235
```
236
236
237
-
What happens here is that `StrArrOrNumArr `distributes on:
237
+
`StrArrOrNumArr `se distribue sur :
238
238
239
239
```ts twoslash
240
240
typeStrArrOrNumArr=
241
241
// ---cut---
242
242
string|number;
243
243
```
244
244
245
-
and maps over each member type of the union, to what is effectively:
245
+
et s'applique à chaque membre de l'union, ce qui donne :
246
246
247
247
```ts twoslash
248
248
typeToArray<Type> =Typeextendsany?Type[] :never;
@@ -251,16 +251,16 @@ type StrArrOrNumArr =
251
251
ToArray<string> |ToArray<number>;
252
252
```
253
253
254
-
which leaves us with:
254
+
En suivant la logique de `ToArray` avec `string` et `number`, on finit par obtenir :
255
255
256
256
```ts twoslash
257
257
typeStrArrOrNumArr=
258
258
// ---cut---
259
259
string[] |number[];
260
260
```
261
261
262
-
Typically, distributivity is the desired behavior.
263
-
To avoid that behavior, you can surround each side of the`extends`keyword with square brackets.
262
+
La distributivité est le comportement voulu d'habitude.
263
+
Pour éviter ce comportement, vous pouvez entourer chaque côté du mot-clé`extends`avec des crochets.
0 commit comments