microsoft / microsoft/TypeScript

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

Offen
#61,706 7 Kommentare 11 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

### ⚙ Compilation target

es2022

### ⚙ Library

lib.es2024.object

### Missing / Incorrect Definition

The type definitions for `Object.groupBy` state that it returns a `Partial>`. This is useful if `K` is a union, enum, etc. - the result is is likely `Partial`, and the result is consistent with a `Partial>` 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` literal than a `Partial>` literal, and any mistakes are more consistently dealt with using TypeScript's `noUncheckedIndexedAccess` option.

See https://github.com/microsoft/TypeScript/pull/56805#issuecomment-2196526425 and https://github.com/microsoft/TypeScript/pull/56805#issuecomment-2385027658 from the PR that originally added types for `Object.groupBy`.

### Sample Code

```TypeScript
type Employee = { name: string, role: 'ic' | 'manager' };
const employees: Set = 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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Start in the lib.es2024.object declarations and inspect the current Object.groupBy type, then find the corresponding library declaration tests. Check how unrestricted string and number keys differ from union or enum keys, and run the relevant TypeScript tests. Done means the broad key cases no longer produce Partial while union-like keys retain appropriate optionality.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
56/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.