Skip to content

Instantly share code, notes, and snippets.

@celian-rib
Last active April 25, 2021 16:42
Show Gist options
  • Save celian-rib/14e4e3d0cb65b30083b8346910873f0b to your computer and use it in GitHub Desktop.
Save celian-rib/14e4e3d0cb65b30083b8346910873f0b to your computer and use it in GitHub Desktop.
Check if variable is of type Interface in TypeScript
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