-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1084 from davidalves1/davidalves1-ptbr-translation
[PT-BR] Translation of some playground example files
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
packages/playground-examples/copy/pt/3-7/Syntax and Messaging/Nullish Coalescing.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//// { compiler: { }, order: 2 } | ||
|
||
// O operador de coalescencia nula é uma alternativa ao || | ||
// que retorna o lado direito da expressão se o lado esquerdo | ||
// é nulo ou undefined (indefinido) | ||
|
||
// Em contraste, || usa a checagem falsy, significando que um texto vazio | ||
// ou o número 0 seriam considerados falso. | ||
|
||
// Um bom exemplo para essa funcionalidade é lidar com | ||
// objetos que tem padrões quando uma chave não é passada. | ||
|
||
interface ConfiguracaoDoApp { | ||
// Padrão: "(sem nome)"; texto vazio é válido | ||
nome: string; | ||
|
||
// Padrão: -1; 0 é válido | ||
itens: number; | ||
|
||
// Padrão: verdadeiro | ||
ativo: boolean; | ||
} | ||
|
||
function updateApp(configuracao: Partial<ConfiguracaoDoApp>) { | ||
// Com o operador null-coalescing | ||
configuracao.nome = configuracao.nome ?? "(sem nome)"; | ||
configuracao.itens = configuracao.itens ?? -1; | ||
configuracao.ativo = configuracao.ativo ?? true; | ||
|
||
// Solução atual | ||
configuracao.nome = typeof configuracao.nome === "string" ? configuracao.nome : "(sem nome)"; | ||
configuracao.itens = typeof configuracao.itens === "number" ? configuracao.itens : -1; | ||
configuracao.ativo = typeof configuracao.ativo === "boolean" ? configuracao.ativo : true; | ||
|
||
// Usando o operador || que poderia oferecer dados ruins | ||
configuracao.nome = configuracao.nome || "(sem nome)"; // não permite a entrada de "" | ||
configuracao.itens = configuracao.itens || -1; // não permite a entrada de 0 | ||
configuracao.ativo = configuracao.ativo || true; // realmente ruim, sempre true (verdadeiro) | ||
} | ||
// Você pode ler mais sobre o operador nullish coalescing no post do blog do 3.7 | ||
// | ||
// https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/ |