Last active
April 3, 2019 14:03
-
-
Save michaeljscript/2d22f0f707ae67cf4a29e553c89e7c6d 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 parsePerson(value: unknown): Person | null { | |
if ( | |
typeof value === "object" && | |
value && | |
value.hasOwnProperty("name") && | |
value.hasOwnProperty("age") && | |
value.hasOwnProperty("something") | |
) { | |
const { name, age, something } = value; | |
if ( | |
typeof name === "string" && | |
typeof age === "number" && | |
typeof something === "string" | |
) { | |
// variables now satisfy the Person interface | |
return { name, age, something }; | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment