Skip to content

Instantly share code, notes, and snippets.

@michaeljscript
Last active April 3, 2019 14:03
Show Gist options
  • Save michaeljscript/2d22f0f707ae67cf4a29e553c89e7c6d to your computer and use it in GitHub Desktop.
Save michaeljscript/2d22f0f707ae67cf4a29e553c89e7c6d to your computer and use it in GitHub Desktop.
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