Skip to content

Instantly share code, notes, and snippets.

@pauleveritt
Created November 18, 2022 19:38
Show Gist options
  • Save pauleveritt/bfd3969f248cd0bb37d5f083f9d72887 to your computer and use it in GitHub Desktop.
Save pauleveritt/bfd3969f248cd0bb37d5f083f9d72887 to your computer and use it in GitHub Desktop.
Component
<h1>Hello Animals</h1>
<ng-container *ngIf="isFish(pet)">
<button (click)="pet.swim()">Swim!</button>
</ng-container>
<ng-container *ngIf="isFish(pet)">
<button (click)="pet.fly()">Fly!</button>
</ng-container>
import {Component, OnInit} from '@angular/core';
abstract class Animal {
eats(): void {
}
}
class Fish extends Animal {
swims(): void {
}
}
class Bird extends Animal {
flys(): void {
}
}
@Component({
selector: 'app-animal',
templateUrl: './animal.component.html',
styleUrls: ['./animal.component.css']
})
export class AnimalComponent implements OnInit {
pet: Animal = new Bird();
constructor() {
}
ngOnInit(): void {
}
isFish(animal: Animal): animal is Fish {
return animal instanceof Fish;
}
isBird(animal: Animal): animal is Bird {
return animal instanceof Bird;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment