1
1
//// { compiler: { }, order: 1 }
2
2
3
- // Optional chaining reached TC39 Stage 3 consensus during
4
- // 3.7's development. Optional Chaining allows you to write
5
- // code which can immediately stop running expressions when
6
- // it hits a null or undefined.
3
+ // Le chaînage optionnel a atteint l'étape 3 du TC39 pendant le development
4
+ // de la version 3.7. Le chaînage optionnel permet d'écrire du code qui va
5
+ // interrompre l'execution des expressions dès qu'il atteint une valeur
6
+ // null ou undefined.
7
7
8
- // Property Access
8
+ // Accès aux propriétés d'un objet
9
9
10
- // Let's imagine we have an album where the artist, and the
11
- // artists bio might not be present in the data. For example
12
- // a compilation may not have a single artist.
10
+ // Imaginons que nous ayons un album où l'artiste, et sa bio, puissent ne pas
11
+ // être present dans les données. Par exemple, une compilation pourrait n'aurait pas un seul artiste.
13
12
14
13
type AlbumAPIResponse = {
15
14
title : string ;
@@ -22,40 +21,40 @@ type AlbumAPIResponse = {
22
21
23
22
declare const album : AlbumAPIResponse ;
24
23
25
- // With optional chaining, you can write
26
- // code like this :
24
+ // Avec le chaînage optionnel,
25
+ // vous pouvez écrire le code suivant :
27
26
28
27
const artistBio = album ?. artist ?. bio ;
29
28
30
- // Instead of :
29
+ // A la place de :
31
30
32
31
const maybeArtistBio = album . artist && album . artist . bio ;
33
32
34
- // In this case ?. acts differently than the &&s since &&
35
- // will act differently on "falsy" values (e.g. an empty string,
36
- // 0, NaN, and, well, false) .
33
+ // Dans ce cas ?. agit différemment que le ET logique (&&) car ce dernier traite
34
+ // les valeur fausses (e.g. une chaîne de caractères vide, 0, Nan et false) de
35
+ // façon différente .
37
36
38
- // Optional chaining will only take null or undefined as
39
- // a signal to stop and return an undefined.
37
+ // Le chaînage optionnel va seulement arrêter l'évaluation et retourner undefined
38
+ // si la valeur est null ou undefined.
40
39
41
- // Optional Element Access
40
+ // Accès à un élément optionnel
42
41
43
- // Property access is via the . operator, the optional chaining
44
- // also works with the [] operators when accessing elements .
42
+ // Acceder à une propriété se fait avec l'opérateur ., et le chaînage optionnel
43
+ // marche aussi avec l'opérateur [] pour acceder à des éléments .
45
44
46
45
const maybeArtistBioElement = album ?. [ "artist" ] ?. [ "bio" ] ;
47
46
48
47
const maybeFirstPreviousAlbum = album ?. artist ?. previousAlbums ?. [ 0 ] ;
49
48
50
- // Optional Calls
49
+ // Appel optionnel
51
50
52
- // When dealing with functions which may or may not exist at
53
- // runtime, optional chaining supports only calling a function
54
- // if it exists. This can replace code where you would traditionally
55
- // write something like: if (func) func()
51
+ // Quand il s'agit d'appeler des fonctions qui peuvent être définies ou non définies,
52
+ // le chaînage optionnel permet d'appeler la fonction uniquement si elle existe.
53
+ // Cela peut remplacer le code où l'on écrirait traditionnellement quelque chose comme:
54
+ // if (func) func()
56
55
57
- // For example here's an optional call to the callback from
58
- // an API request :
56
+ // Par example le chaînage optionnel pour appeler un callback après une requête
57
+ // vers une API :
59
58
60
59
const callUpdateMetadata = ( metadata : any ) => Promise . resolve ( metadata ) ; // Fake API call
61
60
@@ -65,6 +64,6 @@ const updateAlbumMetadata = async (metadata: any, callback?: () => void) => {
65
64
callback ?.( ) ;
66
65
} ;
67
66
68
- // You can read more about optional chaining in the 3.7 blog post :
67
+ // Plus de détails sur le chaînage optionnel dans la version 3.7 sur le blog :
69
68
//
70
69
// https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/
0 commit comments