Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this function's signature mean?

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>
like image 331
gsamaras Avatar asked Jan 25 '26 22:01

gsamaras


1 Answers

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:

enter image description here

like image 70
Ali Shahzad Avatar answered Jan 28 '26 14:01

Ali Shahzad



Donate For Us

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