Last active
April 25, 2021 16:42
-
-
Save celian-rib/14e4e3d0cb65b30083b8346910873f0b to your computer and use it in GitHub Desktop.
Check if variable is of type Interface in TypeScript
This file contains hidden or 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
interface myInterface { | |
myValue: any, | |
} | |
// The variable for which we will need to check the type | |
let myVariable: any | myInterface; | |
// ... | |
//Check is myVariable is of type myInterface | |
if ((myVariable as myInterface).myValue != undefined){ | |
// Yes it is of type myInterface | |
// We can then cast it to access the values | |
const myVarWithMyInterf: myInterface = myVariable as myInterface; | |
console.log(myVarWithMyInterf.myValue); | |
} else { | |
// No it is not | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment