Object.groupBy
should not return Partial<Record<string, T>>
or Partial<Record<number, T>>
#61706
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
⚙ Compilation target
es2022
⚙ Library
lib.es2024.object
Missing / Incorrect Definition
The type definitions for
Object.groupBy<K, T>
state that it returns aPartial<Record<K, T>>
. This is useful ifK
is a union, enum, etc. - the result is is likelyPartial
, and the result is consistent with aPartial<Record<K, T>>
literal, and havingPartial
helps prevent mistakes from assuming that every key is present in the resulting record. However, it's unnecessary ifK
is a unrestricted number or string: there's no way that a declared key within the result can have anundefined
value, and the result is more consistent with aRecord<string | number, T>
literal than aPartial<Record<string | number, T>>
literal, and any mistakes are more consistently dealt with using TypeScript'snoUncheckedIndexedAccess
option.See #56805 (comment) and #56805 (comment) from the PR that originally added types for
Object.groupBy
.Sample Code
Documentation Link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
The text was updated successfully, but these errors were encountered: