Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Dog {
- run: Function
- }
- interface Bird {
- fly: Function
- }
- function myFunction(animal: Dog | Bird) {
- if (animal.fly) {
- animal.fly();
- } else {
- animal.run();
- }
- }
- const dog: Dog = {
- run: function () {
- alert("Let's run dogs");
- }
- };
- const bird: Bird = {
- fly: function () {
- alert("Let's fly birds");
- }
- };
- myFunction(dog); // prints Let's run dogs
- myFunction(bird); // prints Let's fly birds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement