11//// { compiler: { }, order: 1 }
22
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.
77
8- // Property Access
8+ // Accès aux propriétés d'un objet
99
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.
1312
1413type AlbumAPIResponse = {
1514 title : string ;
@@ -22,40 +21,40 @@ type AlbumAPIResponse = {
2221
2322declare const album : AlbumAPIResponse ;
2423
25- // With optional chaining, you can write
26- // code like this :
24+ // Avec le chaînage optionnel,
25+ // vous pouvez écrire le code suivant :
2726
2827const artistBio = album ?. artist ?. bio ;
2928
30- // Instead of :
29+ // A la place de :
3130
3231const maybeArtistBio = album . artist && album . artist . bio ;
3332
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 .
3736
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.
4039
41- // Optional Element Access
40+ // Accès à un élément optionnel
4241
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 .
4544
4645const maybeArtistBioElement = album ?. [ "artist" ] ?. [ "bio" ] ;
4746
4847const maybeFirstPreviousAlbum = album ?. artist ?. previousAlbums ?. [ 0 ] ;
4948
50- // Optional Calls
49+ // Appel optionnel
5150
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()
5655
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 :
5958
6059const callUpdateMetadata = ( metadata : any ) => Promise . resolve ( metadata ) ; // Fake API call
6160
@@ -65,6 +64,6 @@ const updateAlbumMetadata = async (metadata: any, callback?: () => void) => {
6564 callback ?.( ) ;
6665} ;
6766
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 :
6968//
7069// https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/
0 commit comments