Utility for type checking and throwing errors if type does not match.