Last active
February 11, 2023 17:50
-
-
Save fabio-filho/d040f34c61a6cf621c42d951166828c8 to your computer and use it in GitHub Desktop.
typescript-basic review
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
// Variaveis | |
const nome: string = "Fabio"; | |
let idade: number = 1; | |
idade = 2; | |
// Variáveis + null + undefined | |
let nomeQuePodeSerNull: string | null | undefined; | |
nomeQuePodeSerNull = null; | |
// Variáveis + Tipagem | |
const nomeOuIdade: string | number = 28 | |
type ProdutoSituacao = "ruptura" | "avaria" | "isv"; | |
let produtoSituacao1: ProdutoSituacao = "avaria"; | |
produtoSituacao1 = "avaria"; | |
let produtoSituacao2: "ruptura" | "avaria" | "isv"; | |
produtoSituacao2 = "avaria"; | |
// ---------------------------------------- | |
// Objetos | |
const pessoa = { | |
nome: "Gabriel", | |
idade: 26 | |
} | |
// Objetos + Tipagem | |
interface BasePessoa { | |
endereco: string; | |
} | |
interface PessoaInteface extends BasePessoa { | |
nome: string; | |
idade: number; | |
} | |
const pessoaFabio: PessoaInteface = { | |
nome: "Fabio", | |
idade: 28, | |
endereco: 'rua' | |
} | |
type PessoaType = { | |
nome: string; | |
idade: number; | |
} | |
const pessoaGabriel: PessoaType = { | |
nome: "Gabriel", | |
idade: 26 | |
} | |
// Objetos + desconstruição + construção | |
const quarto = { | |
quantidadePortas: 1, | |
quantidadeJanelas: 2 | |
} | |
const sala = { | |
quantidadePortas: 2, | |
quantidadeJanelas: 3 | |
} | |
const cozinha = { | |
quantidadePortas: 3, | |
quantidadeJanelas: 1 | |
} | |
const banheiro = { | |
quantidadePortas: 1, | |
quantidadeJanelas: 1 | |
} | |
const suite = { | |
...quarto, | |
banheiro: { | |
...banheiro, | |
quantidadePortas: 2 | |
} | |
} | |
const casa = { | |
quarto, | |
sala, | |
cozinha, | |
suite, | |
banheiro: { | |
...banheiro, | |
quantidadeJanelas: 2 | |
} | |
} | |
// ---------------------------------------- | |
// Arrays | |
const nomesHomens = ["Fabio", "Gabriel"]; | |
const nomesMulheres = ["Maria", "Marcella"]; | |
// Arrays + Tipagem | |
const idades: number[] = [28, 26]; | |
// Arrays + construção + desconstruição | |
const todosNomes = [...nomesHomens, ...nomesMulheres]; | |
// Array + arrow function | |
const todosNomesComParanteses1 = todosNomes.map((nome) => { | |
return "(" + nome + ")"; | |
}); | |
const todosNomesComParanteses2 = todosNomes.map( | |
(nome) => "(" + nome + ")" | |
); | |
const todosNomesQueComecamComM1 = todosNomes.filter( | |
(nome) => nome.startsWith("M") | |
); | |
const todosNomesQueComecamComM2 = todosNomes.filter((nome) => { | |
return nome.startsWith("M"); | |
}); | |
// ---------------------------------------- | |
// Funções | |
function validarCPF(cpf: string){ | |
throw new Error("Not implemented"); | |
} | |
const validarCEP = (cep: string) => { | |
throw new Error("Not implemented"); | |
} | |
// ---------------------------------------- | |
// Classe | |
class PessoaClasse { | |
constructor( | |
private nome: string, | |
private idade: number, | |
private endereco: string | |
){ } | |
public alterarEndereco(novoEndereco: string) { | |
if (this.endereco === novoEndereco) | |
throw new Error("O nome do endero digitado é o mesmo.") | |
this.endereco = novoEndereco; | |
} | |
static pegarIdadeMinima() { | |
return 20; | |
} | |
} | |
const pessoaClasse = new PessoaClasse("Gabriel", 26, "Rua"); | |
pessoaClasse.alterarEndereco("xuxu") | |
const idadeMinuma = PessoaClasse.pegarIdadeMinima(); | |
// ----------------------- | |
interface NotificadorCelular{ | |
notificar: (mensagem: string) => {} | |
} | |
function getNotificadorCelular(): NotificadorCelular | undefined { | |
return undefined; | |
} | |
function mensagemRecebida(mensagem: string){ | |
// Armazeno a mensagem ... | |
// Faço outras cosias ... | |
const notificadorCelular = getNotificadorCelular(); | |
notificadorCelular?.notificar(mensagem); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment