Created
August 26, 2018 22:11
-
-
Save glaucia86/5057422eb073d2f2344ca1a227fbb79b to your computer and use it in GitHub Desktop.
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
/** | |
* 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