Thanks to visit codestin.com
Credit goes to github.com

Skip to content

add simple test to reproduce groupBy regression #2100

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

Merged
merged 1 commit into from
May 5, 2025

Conversation

giggo1604
Copy link
Contributor

Hello,

While updating our codebase to the latest version of Immutable, we encountered what seems to be a regression in the behavior of groupBy.

This change was introduced in Commit 53237d2.

Previously, grouping by a function that might return undefined was tolerated, but in the latest version, this now throws. While the new behavior arguably makes sense—since grouping by undefined isn’t very clean—it does appear to be a breaking change, violating semantic versioning.

If this change is considered intentional, it should also be reflected in the type system: the grouper function should not be allowed to return undefined.

We’d like to clarify the intended direction:

  • Was this change considered a bug fix?
  • Is there any possibility that the previous behavior might be restored (i.e., loosening the new sanity check)?
  • Or should we assume this is the expected behavior going forward and update our code accordingly?

Thanks in advance for your guidance, and thanks for maintaining the project!

@jdeniau
Copy link
Member

jdeniau commented Apr 29, 2025

Hi,

Thanks for the bug report. I added that because I trusted typescript, but as immutable accept any value as key, having undefined as key should not be a problem.

See https://immutable-js.com/play/#IE1hcChbW3VuZGVmaW5lZCwgJ2EnIF1dICkKLmdldCh1bmRlZmluZWQp

TS might be more strict on undefined values though. I will check that, but it's clearly a regression.
I do not have access to a computer in the next few days though.

It seems reasonable to assert that keys shouldn't be undefined,
since keys are supposed to be of type PropertyKey (string | number | symbol).
But in practice, JavaScript allows undefined as a key by implicitly
converting it to the string "undefined".

This caused a regression specifically in the `groupBy` function, where the
grouper callback can return undefined for some items. That behavior was
previously supported and is now broken by the assertion.

Long-term, it probably makes sense to tighten this up and only allow
real PropertyKeys, but that’ll need a more careful cleanup.
@giggo1604
Copy link
Contributor Author

giggo1604 commented Apr 30, 2025

Hi,

Thanks for your quick answer.

I added a quick fix and a test that caught the regression. Used @ts-expect-error for now since doing a proper fix would probably be a lot more work.

Long-term, it might make more sense to disallow undefined keys and introduce a breaking change. As this is more in line with how typescript works.

@jdeniau jdeniau marked this pull request as ready for review May 5, 2025 21:25
@jdeniau
Copy link
Member

jdeniau commented May 5, 2025

Output diff failing test is under control as we change a TS file that does change the builded output.
This has been set to check that file migration from JS to TS does not alter the output in v5.

@jdeniau jdeniau merged commit 009229d into immutable-js:main May 5, 2025
9 of 11 checks passed
@jdeniau
Copy link
Member

jdeniau commented May 5, 2025

Thanks, this has been released in 5.1.2

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

Successfully merging this pull request may close these issues.

2 participants