Skip to content

Instantly share code, notes, and snippets.

@hw0k
Created January 17, 2021 09:32
Show Gist options
  • Save hw0k/fcd5d476ba340b48482a9fe3a13cd626 to your computer and use it in GitHub Desktop.
Save hw0k/fcd5d476ba340b48482a9fe3a13cd626 to your computer and use it in GitHub Desktop.
TN 2
function sound(animal: Dog | Cat) {
if (animal instanceof Dog) {
// animal은 Dog로 추론됨.
animal.bark();
animal.meow(); // error TS2339: Property 'meow' does not exist on type 'Dog'.
return;
}
if (animal instanceof Cat) {
// animal은 Cat으로 추론됨.
animal.bark(); // error TS2339: Property 'bark' does not exist on type 'Cat'.
animal.meow();
return;
}
// animal은 never로 추론됨.
animal.bark(); // error TS2339: Property 'bark' does not exist on type 'never'.
animal.meow(); // error TS2339: Property 'meow' does not exist on type 'never'.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment