Skip to content

Instantly share code, notes, and snippets.

@hw0k
Created January 18, 2021 05:01
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 hw0k/dfa81575d24fa231a753172e58446750 to your computer and use it in GitHub Desktop.
Save hw0k/dfa81575d24fa231a753172e58446750 to your computer and use it in GitHub Desktop.
TN 5
interface Person {
play: () => void;
sing: () => void;
talk: () => void;
}
function playWithPerson(person: Person | undefined) {
if (!person) {
// person은 undefined로 추론됨.
throw new Error('Person not found!');
}
// person은 Person으로 추론됨.
person.play();
}
function singWithPerson(person: Person | null) {
if (!person) {
// person은 null로 추론됨.
throw new Error('Person not found!');
}
// person은 Person으로 추론됨.
person.sing();
}
function talkWithPerson(person: Person | undefined | null) {
if (!person) {
// person은 undefined | null로 추론됨.
throw new Error('Person not found!');
}
// person은 Person으로 추론됨.
person.talk();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment