Skip to content

Instantly share code, notes, and snippets.

@carlrip
Created December 17, 2019 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlrip/386aeebefb3ffd24130de9ccab199a84 to your computer and use it in GitHub Desktop.
Save carlrip/386aeebefb3ffd24130de9ccab199a84 to your computer and use it in GitHub Desktop.
optional chaining - before
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