Created
December 17, 2019 18:25
-
-
Save carlrip/386aeebefb3ffd24130de9ccab199a84 to your computer and use it in GitHub Desktop.
optional chaining - before
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
type Person = { | |
id: number; | |
name: string; | |
address?: Address; | |
} | |
type Address = { | |
line1: string; | |
line2: string; | |
line3: string; | |
zipcode: string; | |
} | |
function getPersonPostcode(id: number) { | |
const person: Person = getPerson(id); | |
return person.address.zipcode; // 💥 - Type error - Object is possibly 'undefined' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment