Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save charlypoly/a4055b311e232f3e71cd5e5a9fcf2d31 to your computer and use it in GitHub Desktop.
Save charlypoly/a4055b311e232f3e71cd5e5a9fcf2d31 to your computer and use it in GitHub Desktop.
TypeScript optional property (3)
interface Person {
name: string
address?: {
city: string
zipcode: string
}
}
const person: Person = { name: "John", address: undefined }
function printUserInfo(p: Person) {
for (const prop in p) {
console.log(prop, p[prop])
}
}
printUserInfo(person)
// Prints:
// > "name", "John"
// > "address", undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment