Skip to content

HttpParams of New HttpClient Doesn't Support Number, Boolean and Date #23856

@maks-humeniuk

Description

@maks-humeniuk

I'm submitting a...

[x] Regression (a behavior that used to work and stopped working in a new release)

Current behavior

ERROR in /path/to/heroes.service.ts(23,44): error TS2345: Argument of type '{ params: { groupId: number; }; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
  Types of property 'params' are incompatible.
    Type '{ groupId: number; }' is not assignable to type 'HttpParams | { [param: string]: string | string[]; }'.
      Type '{ groupId: number; }' is not assignable to type '{ [param: string]: string | string[]; }'.
        Property 'groupId' is incompatible with index signature.
          Type 'number' is not assignable to type 'string | string[]'.

Expected behavior

No errors should occur.

Minimal reproduction of the problem with instructions

// heroes-query-params.model.ts
export class HeroesQueryParams {
    groupId: number;
    country: string;
    dead: boolean;
}

// heroes.component.ts
const params: HeroesQueryParams = {
    groupId: 1,
    name: 'US',
    dead: false
};
this.heroesService.getHeroes(params).subscribe((heroes: Heroes) => {
    console.log(heroes);
});

// heroes.service.ts
import { HttpClient } from '@angular/common/http';
// ...
getHeroes(params: HeroesQueryParams): Observable<Heroes> {
    return this.httpClient.get<Heroes>('/path/to/heroes', {
        params: params // ERROR
    });
}

To cut a long story short, new HttpClient, which is imported from @angular/common/http, doesn't support non-string values in query params. This is related to numbers, booleans, dates etc. That's why I should use old one, write some transform utils or just .toString().

What is the motivation / use case for changing the behavior?

It worked previously.

Environment

Angular version: 6.0.0

Browser:
- [x] Chrome (desktop) version 66

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4A relatively minor issue that is not relevant to core functionsarea: common/httpIssues related to HTTP and HTTP ClientfeatureLabel used to distinguish feature request from other issuesfreq3: high

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions