Skip to content

prefer-object-from-entries: bad autofix if reduce has a type parameter #2519

@andreww2012

Description

@andreww2012

If an explicit type parameter is present on reduce, it's unsafe for prefer-object-from-entries to fix the problem because this type parameter will carry over to map which will likely be incompatible:

array.reduce<Record<string, Data & { b?: string }>>(
  (res, entry) => ({ ...res, [entry.id]: entry.data }),
  {}
);

// is transformed by the rule to ...

//             but this type parameter v is wrong for `map`
Object.fromEntries(array.map<Record<string, Data & { b?: string }>>(
  ( entry) => [entry.id, entry.data]
));

Full reproduction: https://stackblitz.com/edit/vitejs-vite-axdfht2u

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions