-
-
Save blemoine/2dc489eee7b8d4dd65c57d918522a9c3 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
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