Skip to content

HttpParams get and getAll methods return type does not support number neither boolean #42637

Closed

Description

@aanaya-deltacom

Bug Report

Affected Package

The issue is caused by package @angular/common/http

Is this a regression?

No.

Description

From Angular version 12, the HttpParamsOptions allows number and boolean types in fromObject, introduced with commit #40663

But, the return type for get and getAll methods in HttpParams is string | null and string[] | null respectively.

This causes type errors and tests failing.

Minimal Reproduction

const params = new HttpParams({ fromObject: { numericParam: 5 } });
console.log(
  typeof params.get('numericParam') === 'number', // true
  params.get('numericParam') === '5', // false
  // params.get('numericParam') === 5, // does not compile
);

Your Environment

Angular Version:


Angular CLI: 12.0.3
Node: 14.15.4
Package Manager: npm 6.14.10
OS: linux x64

Angular: 12.0.4
... animations, cdk, common, compiler, compiler-cli, core, forms
... localize, material, platform-browser
... platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1200.3
@angular-devkit/build-angular   12.0.3
@angular-devkit/core            12.0.3
@angular-devkit/schematics      12.0.3
@angular/cli                    12.0.3
@angular/flex-layout            12.0.0-beta.34
@schematics/angular             12.0.3
rxjs                            6.6.7
typescript                      4.2.4

Activity

  1. added
    area: common/http
    on Jun 24, 2021
  2. added this to the needsTriage milestone on Jun 24, 2021
  3. petebacondarwin commented on Jun 24, 2021

    @petebacondarwin
    Contributor

    I don't think this is actually a regression, since in v11 not only can you not pass a number into the HttpParamsOptions, the return type was also always string.

  4. petebacondarwin commented on Jun 24, 2021

    @petebacondarwin
    Contributor

    Actually, moreover, this is by design. The HttpParams is a map of keys to string values. It just happens that you can pass in non-string values when adding to the map, but these values get converted to and stored as strings.

    If you want to compare non-string parameters, I think you would need to use an object or hash Map.

    Closing as working as expected.

  5. aanaya-deltacom commented on Jun 24, 2021

    @aanaya-deltacom
    Author

    Actually, moreover, this is by design. The HttpParams is a map of keys to string values. It just happens that you can pass in non-string values when adding to the map, but these values get converted to and stored as strings.

    If you want to compare non-string parameters, I think you would need to use an object or hash Map.

    Closing as working as expected.

    @petebacondarwin it does not work as expected

    const params = new HttpParams({ fromObject: { numericParam: 5 } });
    console.log(
      typeof params.get('numericParam') === 'number', // true
      params.get('numericParam') === '5', // false
      // params.get('numericParam') === 5, // does not compile
    );
    

    If the parameter was stored as string in the map, then typeof params.get('numericParam') === 'number', // true should be false, isn't it?

  6. petebacondarwin commented on Jun 24, 2021

    @petebacondarwin
    Contributor

    You are right! There is a bug. But not the one reported here.

    We have this code in our constructor:

          Object.keys(options.fromObject).forEach(key => {
            const value = (options.fromObject as any)[key];
            this.map!.set(key, Array.isArray(value) ? value : [value]);
          });

    This should actually be:

          Object.keys(options.fromObject).forEach(key => {
            const value = (options.fromObject as any)[key];
            this.map!.set(key, Array.isArray(value) ? value.map(valueToString) : [valueToString(value)]);
          });

    When adding items subsequently, outside of the constructor, this is what happens to values.

  7. angular-automatic-lock-bot commented on Jul 25, 2021

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  8. locked and limited conversation to collaborators on Jul 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: common/http

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions