Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
andreww2012 opened this issue Dec 25, 2024 · 1 comment
Open

Comments

@andreww2012
Copy link

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

@fregante fregante changed the title prefer-object-from-entries: autofix is unsafe if reduce type parameter is explicitly specified prefer-object-from-entries: bad autofix if reduce has a type parameter Dec 27, 2024
@fregante
Copy link
Collaborator

"unsafe autofix" means that the generated code is not equivalent.

In this case the generated code is just invalid/unexpected, so I changed the title.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants