I am facing a declaration issue - or rather, a challenge in comprehending Typescript. Let me illustrate the scenario:
public migrationSource: Skater | Rink;
public migrationDestination: Skater | Rink;
public migrationMode: MigrationMode;
ngOnInit() {
this.migrationSource = this.migrationMode == MigrationMode.Skater ? new Skater() : new Rink()
this.migrationDestination = this.migrationMode == MigrationMode.Skater ? new Skater() : new Rink()
}
migrateSkater(skater1:Skater, skater2:Skater) {
...
}
migrateRink(rink1:Rink, rink2:Rink) {
...
}
Up to this point, everything seems fine. However, when attempting to invoke the function in my Angular code:
<ng-container *ngIf="migrationMode == mode.SKATER">
<button (click)="migrateSkater(migrationDestination, migrationSource)">
...
</button>
</ng-container>
<ng-container *ngIf="migrationMode == mode.RINK">
<button (click)="migrateRink(migrationDestination, migrationSource)">
...
</button>
</ng-container>
An error message is displayed as follows:
error TS2345: Argument of type 'Skater | Rink' is not assignable to parameter of type 'Skater'.
Type 'Rink' is not assignable to type 'Skater'.
I comprehend the essence of this error message, but I am unsure how to rectify it. Thus far, I resorted to changing the data types of migrateSkater
and migrateRink
to any
, however, this solution does not align with my intention. Is there a method to explicitly define the data types of the functions in Typescript and if so, how can I achieve this?