Skip to content

Commit 2a9dcc3

Browse files
author
mk360
committed
finish everyday types
1 parent c7e043e commit 2a9dcc3

1 file changed

Lines changed: 36 additions & 37 deletions

File tree

docs/documentation/fr/handbook-v2/Everyday Types.md

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -606,21 +606,21 @@ Dans l'exemple ci-dessus, `req.method` est inféré comme étant un `string`, pa
606606

607607
Il y a deux façons de corriger ce problème.
608608

609-
1. You can change the inference by adding a type assertion in either location:
609+
1. Vous pouvez ajouter une assertion à deux endroits :
610610

611611
```ts twoslash
612612
declare function handleRequest(url: string, method: "GET" | "POST"): void;
613613
// ---cut---
614-
// Change 1:
614+
// 1er endroit :
615615
const req = { url: "https://example.com", method: "GET" as "GET" };
616-
// Change 2
616+
// 2ème endroit
617617
handleRequest(req.url, req.method as "GET");
618618
```
619619

620-
Change 1 means "I intend for `req.method` to always have the _literal type_ `"GET"`", preventing the possible assignment of `"GUESS"` to that field after.
621-
Change 2 means "I know for other reasons that `req.method` has the value `"GET"`".
620+
Le premier changement signifie "Je veux que `req.method` ait toujours le type littéral `"GET"`", empêchant tout changement de cette valeur vers `"GUESS"` plus tard.
621+
Le deuxième changement signifie "Je sais que, pour certaines raisons, `req.method` est égal à `"GET"`".
622622

623-
2. You can use `as const` to convert the entire object to be type literals:
623+
2. Vous pouvez utiliser `as const` pour convertir l'objet entier en types littéraux :
624624

625625
```ts twoslash
626626
declare function handleRequest(url: string, method: "GET" | "POST"): void;
@@ -629,86 +629,85 @@ Il y a deux façons de corriger ce problème.
629629
handleRequest(req.url, req.method);
630630
```
631631

632-
The `as const` suffix acts like `const` but for the type system, ensuring that all properties are assigned the literal type instead of a more general version like `string` or `number`.
632+
Le suffixe `as const` vaut la même chose que `const` mais pour le système de types, garantissant que toutes les propriétés aient un type littéral, au lieu d'un type plus général comme `string` ou `number`.
633633

634-
## `null` and `undefined`
634+
## `null` et `undefined`
635635

636-
JavaScript has two primitive values used to signal absent or uninitialized value: `null` and `undefined`.
636+
JavaScript a deux valeurs primitives pour signaler une valeur inexistante ou non-initialisée : `null` et `undefined`.
637637

638-
TypeScript has two corresponding _types_ by the same names. How these types behave depends on whether you have the [`strictNullChecks`](/tsconfig#strictNullChecks) option on.
638+
TypeScript a deux _types_ des mêmes noms. Le comportement de ces types dépend de l'activation de l'option [`strictNullChecks`](/tsconfig#strictNullChecks).
639639

640-
### `strictNullChecks` off
640+
### `strictNullChecks` désactivé
641641

642-
With [`strictNullChecks`](/tsconfig#strictNullChecks) _off_, values that might be `null` or `undefined` can still be accessed normally, and the values `null` and `undefined` can be assigned to a property of any type.
643-
This is similar to how languages without null checks (e.g. C#, Java) behave.
644-
The lack of checking for these values tends to be a major source of bugs; we always recommend people turn [`strictNullChecks`](/tsconfig#strictNullChecks) on if it's practical to do so in their codebase.
642+
Si [`strictNullChecks`](/tsconfig#strictNullChecks) est _désactivé_, les valeurs qui pourraient être `null` ou `undefined` peuvent toujours être lues, et les valeurs `null` et `undefined` peuvent être assignées à des variables de tous types.
643+
Ce comportement est similaire aux langages qui n'ont pas de vérification pour `null` ou `undefined` (ex. Java ou C#).
644+
Ne pas vérifier pour ces deux valeurs tend à être une source importante de bugs ; nous recommandons toujours d'activer [`strictNullChecks`](/tsconfig#strictNullChecks) s'il est pratique de le faire.
645645

646-
### `strictNullChecks` on
646+
### `strictNullChecks` activé
647647

648-
With [`strictNullChecks`](/tsconfig#strictNullChecks) _on_, when a value is `null` or `undefined`, you will need to test for those values before using methods or properties on that value.
649-
Just like checking for `undefined` before using an optional property, we can use _narrowing_ to check for values that might be `null`:
648+
Si [`strictNullChecks`](/tsconfig#strictNullChecks) est _activé_, quand une valeur est `null` ou `undefined`, vous devrez vous assurer que ces deux types sont écartés avant d'utiliser des méthodes ou des propriétés sous ces valeurs.
649+
Tout comme `undefined` doit être éliminé avant d'utiliser une propriété facultative, vous pouvez vous servir du _rétrécissement_ pour éliminer les valeurs qui pourraient être `null` :
650650

651651
```ts twoslash
652652
function doSomething(x: string | null) {
653653
if (x === null) {
654-
// do nothing
654+
// ne rien faire
655655
} else {
656-
console.log("Hello, " + x.toUpperCase());
656+
console.log("Bonjour, " + x.toUpperCase());
657657
}
658658
}
659659
```
660660

661-
### Non-null Assertion Operator (Postfix `!`)
661+
### Opérateur d'assertion non-nulle (Suffixe `!`)
662662

663-
TypeScript also has a special syntax for removing `null` and `undefined` from a type without doing any explicit checking.
664-
Writing `!` after any expression is effectively a type assertion that the value isn't `null` or `undefined`:
663+
TypeScript possède une syntaxe spéciale pour éliminer `null` et `undefined` d'un type sans passer par un rétrécissement.
664+
Écrire `!` après toute expression est effectivement une assertion que cette valeur n'est ni `null`, ni `undefined` :
665665

666666
```ts twoslash
667667
function liveDangerously(x?: number | null) {
668-
// No error
668+
// Pas d'erreur
669669
console.log(x!.toFixed());
670670
}
671671
```
672672

673-
Just like other type assertions, this doesn't change the runtime behavior of your code, so it's important to only use `!` when you know that the value _can't_ be `null` or `undefined`.
673+
Tout comme les autres assertions, votre code ne sera pas changé à l'exécution, donc n'utilisez l'opérateur `!` que si vous savez que votre type _ne peut jamais_ être `null` ni `undefined`.
674674

675675
## Enums
676676

677-
Enums are a feature added to JavaScript by TypeScript which allows for describing a value which could be one of a set of possible named constants. Unlike most TypeScript features, this is _not_ a type-level addition to JavaScript but something added to the language and runtime. Because of this, it's a feature which you should know exists, but maybe hold off on using unless you are sure. You can read more about enums in the [Enum reference page](/docs/handbook/enums.html).
677+
Les Enums sont une fonctionnalité ajoutée à JavaScript par TypeScript. Elle permet de décrire une valeur qui pourrait faire partie d'un ensemble de constantes nommées. Au contraire de la plupart des fonctionnalités TypeScript, elle n'est _pas_ un ajout au niveau du système de types, mais bel et bien un ajout qui sera reflété à l'exécution. De ce fait, vous devriez savoir que cette possibilité existe, mais vous ne devriez vous en servir que si vous en avez la certitude. Vous pouvez lire plus dans la [page de référence d'Enums](/fr/docs/handbook/enums.html).
678678

679-
## Less Common Primitives
679+
## Primitives moins fréquentes
680680

681-
It's worth mentioning the rest of the primitives in JavaScript which are represented in the type system.
682-
Though we will not go into depth here.
681+
Il serait intéressant de mentionner le reste des primitives JavaScript représentées dans le système de types, bien que nous ne rentrerons pas dans leurs détails.
683682

684683
#### `bigint`
685684

686-
From ES2020 onwards, there is a primitive in JavaScript used for very large integers, `BigInt`:
685+
À partir d'ES2020, il existe une primitive JavaScript pour les très grands entiers, `BigInt`:
687686

688687
```ts twoslash
689688
// @target: es2020
690689

691-
// Creating a bigint via the BigInt function
690+
// Création d'un BigInt via la fonction
692691
const oneHundred: bigint = BigInt(100);
693692

694-
// Creating a BigInt via the literal syntax
693+
// Création d'un BigInt via la syntaxe littérale
695694
const anotherHundred: bigint = 100n;
696695
```
697696

698-
You can learn more about BigInt in [the TypeScript 3.2 release notes](/docs/handbook/release-notes/typescript-3-2.html#bigint).
697+
Vous pouvez en apprendre plus sur les BigInt dans les [notes de changement de TypeScript 3.2](/docs/handbook/release-notes/typescript-3-2.html#bigint).
699698

700699
#### `symbol`
701700

702-
There is a primitive in JavaScript used to create a globally unique reference via the function `Symbol()`:
701+
Il existe une primitive en JavaScript qui sert à créer des références uniques via la fonction `Symbol()`:
703702

704703
```ts twoslash
705704
// @errors: 2367
706-
const firstName = Symbol("name");
707-
const secondName = Symbol("name");
705+
const firstName = Symbol("nom");
706+
const secondName = Symbol("nom");
708707

709708
if (firstName === secondName) {
710-
// Can't ever happen
709+
// Ne peut jamais se produire
711710
}
712711
```
713712

714-
You can learn more about them in [Symbols reference page](/docs/handbook/symbols.html).
713+
Vous pouvez en savoir plus dans la [page de référence des Symbols](/fr/docs/handbook/symbols.html).

0 commit comments

Comments
 (0)