Created
November 29, 2021 11:33
-
-
Save charlypoly/a4055b311e232f3e71cd5e5a9fcf2d31 to your computer and use it in GitHub Desktop.
TypeScript optional property (3)
This file contains hidden or 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
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