Skip to content

Object.groupBy should not return Partial<Record<string, T>> or Partial<Record<number, T>> #61706

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

Open
joshkel opened this issue May 15, 2025 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@joshkel
Copy link

joshkel commented May 15, 2025

⚙ Compilation target

es2022

⚙ Library

lib.es2024.object

Missing / Incorrect Definition

The type definitions for Object.groupBy<K, T> state that it returns a Partial<Record<K, T>>. This is useful if K is a union, enum, etc. - the result is is likely Partial, and the result is consistent with a Partial<Record<K, T>> literal, and having Partial helps prevent mistakes from assuming that every key is present in the resulting record. However, it's unnecessary if K is a unrestricted number or string: there's no way that a declared key within the result can have an undefined value, and the result is more consistent with a Record<string | number, T> literal than a Partial<Record<string | number, T>> literal, and any mistakes are more consistently dealt with using TypeScript's noUncheckedIndexedAccess option.

See #56805 (comment) and #56805 (comment) from the PR that originally added types for Object.groupBy.

Sample Code

type Employee = { name: string, role: 'ic' | 'manager' };
const employees: Set<Employee> = new Set();

const byName = Object.groupBy(employees, x => x.name);
for (const [name, nameGroup] of byName) {
  // TypeScript gives an error that nameGroup may be undefined,
  // but it's always defined
  console.log(nameGroup.length);
}

Documentation Link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy

@joshkel
Copy link
Author

joshkel commented May 15, 2025

A possible implementation of this change:

/**
 * Identifies unrestricted strings and numbers (i.e., not unions, not numeric
 * enums which can broaden to numbers, etc.).
 */
type IsAnyStringOrNumber<K extends PropertyKey> = K extends string | number
  ? string extends K
    ? K
    : `${number}` extends `${K}`
    ? K
    : string | number extends K
    ? K
    : never
  : never;

interface ObjectConstructor {
    /**
     * Groups members of an iterable according to the return value of the passed callback.
     * @param items An iterable.
     * @param keySelector A callback which will be invoked for each item in items.
     */
    groupBy<K extends PropertyKey, T>(
        items: Iterable<T>,
        keySelector: (item: T, index: number) => K,
    ): [K] extends [IsAnyStringOrNumber<K>] ? Record<K, T[]> : Partial<Record<K, T[]>>;
}

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented May 15, 2025

Changing this would introduce a confusing variance break where you can have code that is legal become illegal when an input type becomes narrower, which is usually not possible.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels May 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy