Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator '+' cannot be applied to types 'Number' and '1'

I am getting an error when Operator '+' cannot be applied to types 'Number' and '1'

buildQuerySpec() {
  return {
    PageSize: this.paging.PageCount,
    CurrentPage: this.paging.PageIndex + 1,
    MaxSize: '',
    Filters: this.filter,
    OrderFields: [],
    IsDescending: false
  };
}

what is wrong with

 CurrentPage: this.paging.PageIndex + 1,

pageIndex is number , no idea really.

like image 445
Arash Avatar asked Nov 25 '17 09:11

Arash


3 Answers

Googling the error message leads you to https://github.com/Microsoft/TypeScript/issues/2031 which pretty much explains the reason why it does not work.

You can also have a look at the Do's and Don'ts Section:

Number, String, Boolean, and Object

Don’t ever use the types Number, String, Boolean, or Object. These types refer to non-primitive boxed objects that are almost never used appropriately in JavaScript code.

/* WRONG */
function reverse(s: String): String;

Do use the types number, string, and boolean.

/* OK */
function reverse(s: string): string;

In other words, replace the type Number with number.

like image 182
str Avatar answered Nov 10 '22 15:11

str


Alternatively, you can try adding the unary operator + like,

CurrentPage: +this.paging.PageIndex + 1

which will also work in your case.

Hope this helps!

like image 37
David R Avatar answered Nov 10 '22 15:11

David R


Just replace Number with number wherever you are defining the type for PageIndex in your code.

like image 36
Anthony Avila Avatar answered Nov 10 '22 16:11

Anthony Avila