Skip to content

Instantly share code, notes, and snippets.

@glaucia86
Created August 26, 2018 22:11
Show Gist options
  • Save glaucia86/5057422eb073d2f2344ca1a227fbb79b to your computer and use it in GitHub Desktop.
Save glaucia86/5057422eb073d2f2344ca1a227fbb79b to your computer and use it in GitHub Desktop.
/**
* Arquivo: unknown-type.ts
* Author: Glaucia Lemos
* Data: 23/08/2018
*/
// Agora sim.. vai funfar!!
let textoExemplo: unknown = 10;
// Aqui vamos colocar com o tipo ‘any’
function verificarTipos(obj: any): obj is { x: any, y: any, z: any } {
return !!obj &&
typeof obj === 'object' &&
'x' in obj &&
'y' in obj &&
'z' in obj;
}
// Agora ficarão as propriedades ficarão acessíveis:
if(verificarTipos(textoExemplo)) {
textoExemplo.x.prop;
textoExemplo.y.prop;
textoExemplo.z.prop;
}
upperCase(textoExemplo as string);
function upperCase(x: string) {
return x.toUpperCase();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment