Skip to content

Regression - Array.filterΒ #50562

Closed
Closed

Description

Bug Report

Array filter have possibly wrong signature or 4.8 introduced "Unconstrained Generics No Longer Assignable to {}" introduced type error

πŸ”Ž Search Terms

image

πŸ•— Version & Regression Information

After the update to 4.8.2

⏯ Playground Link

Playground - long cause copy some angular types for perfect reproduction - actual code on the end of code

πŸ’» Code

export const serializeParams = <T extends {}>(query: T): HttpParams => {
  return new HttpParams().appendAll(
        Object.fromEntries(Object.entries(query))
        )
};


export const serializeParams2 = <T extends {}>(query: T): HttpParams => {
      return new HttpParams().appendAll(
        Object.fromEntries(Object.entries(query).filter(val => val[1] !== undefined))
    )
};


export const serializeParams3 = <T extends {}>(query: T): HttpParams => {
  return new HttpParams().appendAll(Object.fromEntries(Object.entries(query)
    .filter(([_, value]) => value !== undefined)
    .map(([key, value]) => {
      if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
        return [key, JSON.stringify(value)];
      } else {
        return [key, value ?? ''];
      }
    })));
};


export const serializeParams4 = <T extends {}>(query: T): HttpParams => {
  const entries = Object.entries(query)
    .filter(([_, value]) => value !== undefined)
    .map(([key, value]) => {
      if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
        return [key, JSON.stringify(value)];
      } else {
        return [key, value ?? ''];
      }
    });

  return new HttpParams().appendAll(Object.fromEntries(entries)); // here very weird - moving body to separate const fixes type error in VS code but build still fails with error
};

πŸ™ Actual behavior

unfiltered and filtered arrays behave differently

πŸ™‚ Expected behavior

unfiltered and filtered arrays behave the same

Activity

  1. jcalz commented on Aug 31, 2022

    @jcalz
    Contributor

    πŸ”Ž Search Terms

    This should probably be replaced with a list of terms people can search for to find this issue and related issues.

  2. RyanCavanaugh commented on Aug 31, 2022

    @RyanCavanaugh
    Member

    I'm not seeing a difference between 4.7 and 4.8 in the Playground link here. Regardless, we'd need a shorter repro to be able to investigate this

  3. kbrilla commented on Sep 1, 2022

    @kbrilla
    Author

    I'm working on clearer and shorter reproduction, will try to post it today

  4. graphemecluster commented on Sep 1, 2022

    @graphemecluster
    Contributor

    What the OP mean is:

    declare function f(arg: Record<string, string>): void;
    
    function foo<T extends {}>(arg: T) {
        f(Object.fromEntries(Object.entries(arg).filter(val => val[1] !== undefined))); // ts(2345)
    }
    
    function bar<T extends {}>(arg: T) {
        f(Object.fromEntries(Object.entries(arg))); // OK
    }
  5. MartinJohns commented on Sep 1, 2022

    @MartinJohns
    Contributor

    graphemecluster (@graphemecluster) How is that an regression in 4.8? It's the same error in the last few versions.

  6. graphemecluster commented on Sep 1, 2022

    @graphemecluster
    Contributor

    Martin Johns (@MartinJohns) Yes, I can confirm that. All the versions I tried raise the same error. I didn't agree with the OP either. (I am just a passer-by.)

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

    Needs More InfoThe issue still hasn't been fully clarified

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions