Skip to content

Instantly share code, notes, and snippets.

@blemoine
Last active March 23, 2017 16:36
Show Gist options
  • Save blemoine/2dc489eee7b8d4dd65c57d918522a9c3 to your computer and use it in GitHub Desktop.
Save blemoine/2dc489eee7b8d4dd65c57d918522a9c3 to your computer and use it in GitHub Desktop.
function isUser(o: any): o is User {
return o && !!o.firstName && !!o.lastName && !!o.age;
}
const maybeUser: any = null;
if (isUser(maybeUser)) {
//We don't have to cast maybeUser
//TypeScript know that in this 'if', maybeUser is a User
console.log(maybeUser.firstName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment