I am following this tutorial, which has a live example and it defines this function:
onSelect(hero: Hero): void {
this.selectedHero = hero;
}
where : void is the part I don't understand. How is this part called, and what does it do?
My idea: It's the return type of the function, but if that was the case, shouldn't
onSelect(hero: Hero): void {
this.selectedHero = hero;
return hero;
}
throw an error? The app is working fine in Plunker and no error is shown in the console (I wonder though whether this has to do with Plunker somehow).
The function is called like this:
<li *ngFor="let hero of heroes"
[class.selected]="hero === selectedHero"
(click)="onSelect(hero)">
<span class="badge">{{hero.id}}</span> {{hero.name}}
</li>
Your idea is correct. It's simply a return type just like functions in other programming languages like C#. And the case when you are returning object of type Hero, it should give an error like this Type 'Hero' is not assignable to type 'void', as shown in the following picture:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With