运算符“ +”不能应用于类型“ Number”和“ 1”

Ara*_*ash 5 typescript angular

无法将运算符“ +”应用于类型“ Number”和“ 1”时出现错误

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

出什么问题了

 CurrentPage: this.paging.PageIndex + 1,
Run Code Online (Sandbox Code Playgroud)

pageIndex是数字,真的不知道。

str*_*str 9

搜寻错误消息会导致您转到https://github.com/Microsoft/TypeScript/issues/2031,这几乎解释了它不起作用的原因。

您还可以查看“注意事项”部分

数字,字符串,布尔值和对象

永远不要使用类型NumberStringBoolean,或Object。这些类型指的是非原始的盒装对象,这些对象几乎从未在JavaScript代码中正确使用过。

/* WRONG */
function reverse(s: String): String;
Run Code Online (Sandbox Code Playgroud)

确实使用类型numberstringboolean

/* OK */
function reverse(s: string): string;
Run Code Online (Sandbox Code Playgroud)

换句话说,替换类型Numbernumber


Dav*_*d R 7

另外,您可以尝试添加一元运算符,+例如

CurrentPage: +this.paging.PageIndex + 1

这也适用于您的情况。

希望这可以帮助!