预期至少0个参数,但得到2个

时间:2018-09-28 07:46:10

标签: angular typescript arguments

我从以下代码中收到Expected 0 type arguments, but got 2上的类型错误updateRole()

account-endpoint.service.ts

getRoleByRoleNameEndpoint<T>(roleName: string): Observable<T> {
    let endpointUrl = `${this.roleByRoleNameUrl}/${roleName}`;

    return this.http.get<T>(endpointUrl, this.getRequestHeaders()).pipe<T>(
      catchError(error => {
        return this.handleError(error, () => this.getRoleByRoleNameEndpoint(roleName));
      }));
  }

  getUpdateRoleEndpoint<T>(roleObject: any, roleId: string): Observable<T> {
    let endpointUrl = `${this.rolesUrl}/${roleId}`;

    return this.http.put<T>(endpointUrl, JSON.stringify(roleObject), this.getRequestHeaders()).pipe<T>(
      catchError(error => {
        return this.handleError(error, () => this.getUpdateRoleEndpoint(roleObject, roleId));
      }));
  }

account.service.ts

private onRolesChanged(roles: Role[] | string[], op: RolesChangedOperation) {
    this._rolesChanged.next({ roles: roles, operation: op });
  }

  updateRole(role: Role) {
    if (role.id) {
      return this.accountEndpoint.getUpdateRoleEndpoint(role, role.id).pipe(
        tap(data => this.onRolesChanged([role], AccountService.roleModifiedOperation)));
    } else {
      return this.accountEndpoint.getRoleByRoleNameEndpoint<Role>(role.name).pipe<Role>(
        mergeMap((foundRole: Role) => {
          role.id = foundRole.id;
          return this.accountEndpoint.getUpdateRoleEndpoint(role, role.id);
        }),
        tap(data => this.onRolesChanged([role], AccountService.roleModifiedOperation)));
    }
  }

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

我可能正在弥补这一点,但我想我发现Typescript有时在显示类型参数错误的真正来源/原因方面做得很差。我不确定这是否是导致您出现问题的原因,但是您似乎在getRoleByRoleNameEndpoint方法定义中缺少类型参数。

getRoleByRoleNameEndpoint<T>(roleName: string): Observable<T> {
  let endpointUrl = `${this.roleByRoleNameUrl}/${roleName}`;

  return this.http.get<T>(endpointUrl, this.getRequestHeaders()).pipe<T>(
    catchError(error => {
      return this.handleError(error, () => this.getRoleByRoleNameEndpoint(roleName));

                   // looks like this should have a type argument here ^
                   // unless I'm missing somehow it can be inferred
    })
  );
}