Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript returning Argument of type 'void' is not assignable to parameter of type 'TimerHandler?

I want to update the count of number of message received by use on live server in angular 7 like we have in facebook and linkedin on main page and i have tried the following code:-

the code to fetch message:-

getMessageCount(rid:any)
    {
      return this.http.get<any>(this.apiUrl+'message/'+rid);
    }
on the main page we have the following code run like this :-
export class HomeComponent implements OnInit {
   userAuthenticated=false;
   private tmessage:any;
  constructor(private service:LoginService,private mService:MessagingService) {
    if(this.service.currentUserValue)
    {
      this.userAuthenticated=true;
      const id=this.service.currentUserValue.id;
      window.setInterval(this.getMessages(id),5);

    }
  }
}

upon sending the new message it should be updated without reloading the page ,can anybody tell me what i can do to achieve it ?

like image 639
Atul kumar singh Avatar asked Apr 07 '19 14:04

Atul kumar singh


People also ask

Is not assignable to parameter of type void TypeScript?

The "Type 'void' is not assignable to type" TypeScript error occurs when we forget to return a value from a function, so the function gets an implicit return type of void . To solve the error, make sure you return a value of the correct type from your functions before the assignment.

How do I create a void function in TypeScript?

The syntax (a: string) => void means “a function with one parameter, named a , of type string, that doesn't have a return value”. Just like with function declarations, if a parameter type isn't specified, it's implicitly any . Note that the parameter name is required.

How do you pass a function as a parameter TypeScript?

Similar to JavaScript, to pass a function as a parameter in TypeScript, define a function expecting a parameter that will receive the callback function, then trigger the callback function inside the parent function.

Is not assignable to parameter of type never?

The error "Argument of type is not assignable to parameter of type 'never'" occurs when we declare an empty array without explicitly typing it and attempt to add elements to it. To solve the error, explicitly type the empty array, e.g. const arr: string[] = []; .


2 Answers

You didn't provide getMessages, so I have to guess.

My guess is, getMessages is a void function, doing something, probably updating model/view and not returning anything, while the first parameter of setTimeout should be a function. Therefore, this

      window.setInterval(this.getMessages(id), 5);

should be

      window.setInterval(() => this.getMessages(id), 5);
like image 155
mbojko Avatar answered Sep 23 '22 09:09

mbojko


The problem is setInterval() needs a function as the first argument(also called as a callback function). Then it executes that function after the set amount of time. And also not only run the function once as setTimeout() but regularly after the given interval of time. But you are not giving a function in your code

window.setInterval(this.getMessages(id), 5);

but just giving a value return by the function getMessages(id).

So the correct way should be

window.setInterval(() => this.getMessages(id), 5);

which now gives a function that can be executed after the given amount of time.

And there is another method where you can call the function by function.call().

window.setInterval(() => this.getMessages.call(this, id), 5)

call() method calls a function with a given this value and arguments provided individually.

Hope this will help you. (Please correct me if there is anything wrong. Thanks in Advance)

like image 30
Isuru Maldeniya Avatar answered Sep 24 '22 09:09

Isuru Maldeniya