Skip to content

Revert previous assertion as it introduced a regression #2102

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 2 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions __tests__/groupBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ describe('groupBy', () => {
expect(group.toJS()).toEqual({ odd: [1, 3, 5], even: [2, 4, 6] });
});

it('allows `undefined` as a key', () => {
const group = Seq([1, 2, 3, 4, 5, 6]).groupBy((x) =>
x % 2 ? undefined : 'even'
);
expect(group.toJS()).toEqual({ undefined: [1, 3, 5], even: [2, 4, 6] });
});

it('groups indexed sequences, maintaining indicies when keyed sequences', () => {
const group = Seq([1, 2, 3, 4, 5, 6]).groupBy((x) => x % 2);

Expand Down
4 changes: 2 additions & 2 deletions src/functional/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ export function get<V, NSV>(
key: string,
notSetValue: NSV
): V | NSV;
export function get<K extends PropertyKey, V, NSV>(
export function get<K, V, NSV>(
collection: Collection<K, V> | Array<V> | { [key: string]: V },
key: K,
notSetValue?: NSV
): V | NSV;
export function get<K extends PropertyKey, V, NSV>(
export function get<K, V, NSV>(
collection: Collection<K, V> | Array<V> | { [key: string]: V },
key: K,
notSetValue?: NSV
Expand Down
6 changes: 4 additions & 2 deletions src/functional/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ export function remove<
K extends keyof C,
>(collection: C, key: K): C;
export function remove<
K extends PropertyKey,
K,
C extends
| Collection<K, unknown>
| Array<unknown>
| { [key: PropertyKey]: unknown },
>(collection: C, key: K): C;
export function remove<K extends PropertyKey>(
export function remove<K>(
collection:
| Collection<K, unknown>
| Array<unknown>
Expand All @@ -67,6 +67,7 @@ export function remove<K extends PropertyKey>(
// @ts-expect-error weird "remove" here,
return collection.remove(key);
}
// @ts-expect-error assert that key is a string, a number or a symbol here
if (!hasOwnProperty.call(collection, key)) {
return collection;
}
Expand All @@ -75,6 +76,7 @@ export function remove<K extends PropertyKey>(
// @ts-expect-error assert that key is a number here
collectionCopy.splice(key, 1);
} else {
// @ts-expect-error assert that key is a string, a number or a symbol here
delete collectionCopy[key];
}
return collectionCopy;
Expand Down
45 changes: 13 additions & 32 deletions src/functional/updateIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,19 @@ export type PossibleCollection<K, V, TProps extends object> =
| Record<TProps>
| Array<V>;

type UpdaterFunction<K extends PropertyKey, C> = (
type UpdaterFunction<K, C> = (
value: RetrievePath<C, Array<K>> | undefined
) => unknown | undefined;
type UpdaterFunctionWithNSV<K extends PropertyKey, C, NSV> = (
type UpdaterFunctionWithNSV<K, C, NSV> = (
value: RetrievePath<C, Array<K>> | NSV
) => unknown;

export function updateIn<K extends PropertyKey, V, C extends Collection<K, V>>(
export function updateIn<K, V, C extends Collection<K, V>>(
collection: C,
keyPath: KeyPath<K>,
updater: UpdaterFunction<K, C>
): C;
export function updateIn<
K extends PropertyKey,
V,
C extends Collection<K, V>,
NSV,
>(
export function updateIn<K, V, C extends Collection<K, V>, NSV>(
collection: C,
keyPath: KeyPath<K>,
notSetValue: NSV,
Expand All @@ -75,51 +70,42 @@ export function updateIn<
notSetValue: NSV,
updater: UpdaterFunctionWithNSV<K, C, NSV>
): C;
export function updateIn<K extends PropertyKey, V, C extends Array<V>>(
export function updateIn<K, V, C extends Array<V>>(
collection: C,
keyPath: KeyPath<string | number>,
updater: UpdaterFunction<K, C>
): Array<V>;
export function updateIn<K extends PropertyKey, V, C extends Array<V>, NSV>(
export function updateIn<K, V, C extends Array<V>, NSV>(
collection: C,
keyPath: KeyPath<K>,
notSetValue: NSV,
updater: UpdaterFunctionWithNSV<K, C, NSV>
): Array<V>;
export function updateIn<K extends PropertyKey, C>(
export function updateIn<K, C>(
object: C,
keyPath: KeyPath<K>,
updater: UpdaterFunction<K, C>
): C;
export function updateIn<K extends PropertyKey, C, NSV>(
export function updateIn<K, C, NSV>(
object: C,
keyPath: KeyPath<K>,
notSetValue: NSV,
updater: UpdaterFunctionWithNSV<K, C, NSV>
): C;
export function updateIn<
K extends PropertyKey,
V,
C extends { [key: PropertyKey]: V },
>(
export function updateIn<K, V, C extends { [key: PropertyKey]: V }>(
collection: C,
keyPath: KeyPath<K>,
updater: UpdaterFunction<K, C>
): { [key: PropertyKey]: V };
export function updateIn<
K extends PropertyKey,
V,
C extends { [key: PropertyKey]: V },
NSV,
>(
export function updateIn<K, V, C extends { [key: PropertyKey]: V }, NSV>(
collection: C,
keyPath: KeyPath<K>,
notSetValue: NSV,
updater: UpdaterFunction<K, C>
): { [key: PropertyKey]: V };

export function updateIn<
K extends PropertyKey,
K,
V,
TProps extends object,
C extends PossibleCollection<K, V, TProps>,
Expand Down Expand Up @@ -150,7 +136,7 @@ export function updateIn<
}

function updateInDeeply<
K extends PropertyKey,
K,
TProps extends object,
C extends PossibleCollection<unknown, unknown, TProps>,
NSV,
Expand Down Expand Up @@ -180,12 +166,6 @@ function updateInDeeply<
}
const key = keyPath[i];

if (typeof key === 'undefined') {
throw new TypeError(
'Index can not be undefined in updateIn(). This should not happen'
);
}

const nextExisting = wasNotSet ? NOT_SET : get(existing, key, NOT_SET);
const nextUpdated = updateInDeeply(
nextExisting === NOT_SET ? inImmutable : isImmutable(nextExisting),
Expand All @@ -196,6 +176,7 @@ function updateInDeeply<
notSetValue,
updater
);

return nextUpdated === nextExisting
? existing
: nextUpdated === NOT_SET
Expand Down
2 changes: 1 addition & 1 deletion type-definitions/immutable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ declare namespace Immutable {
never;

/** @ignore */
type RetrievePath<R, P extends ReadonlyArray<PropertyKey>> = P extends []
type RetrievePath<R, P extends ReadonlyArray<unknown>> = P extends []
? P
: RetrievePathReducer<R, Head<P>, Tail<P>>;

Expand Down
Loading