-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditionalTypes.ts
48 lines (35 loc) · 992 Bytes
/
conditionalTypes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// ==> Exemplo 01 - Conditional Types
type Pessoa = {
nome: string;
idade: number;
endereco: string;
};
type Empresa = {
nome: string;
cnpj: number;
};
type EnderecoPessoa = {
enderecoSecundario: string;
cidade: string;
pais: string;
};
type EnderecoEmpresa = {
localizacao: 'rua' | 'avenida' | 'praça';
};
type EnderecoFinal<T> = T extends { endereco: string } ? EnderecoPessoa : EnderecoEmpresa;
const enderecoPessoa: EnderecoFinal<Pessoa> = {
enderecoSecundario: 'Rua 2',
cidade: 'São Paulo',
pais: 'Brasil',
};
const enderecoEmpresa: EnderecoFinal<Empresa> = {
localizacao: 'rua',
};
console.log(enderecoPessoa);
console.log(enderecoEmpresa);
// ==> Exemplo 02 - Conditional Types ()
type FormatoArquivos = 'png' | 'jpg' | 'gif' | 'svg' | 'mp4' | 'mp3';
type FiltrarArquivoAudio<T> = T extends 'mp4' | 'mp3' ? T : never;
type ArquivoAudio = FiltrarArquivoAudio<FormatoArquivos>;
const arquivoAudio: ArquivoAudio = 'mp4';
console.log(arquivoAudio);