Make type signature of groupBy more accurate#272395
Conversation
See #272263, it currently assumes that all keys of K will be defined. This still isn't perfect, because it says that the returned object might contain a key with value `undefined`, but in reality, if a key is defined, it will have a value. So now usages of this with Object.entries and so on are slighly wrong, but that's preferable IMO.
There was a problem hiding this comment.
Pull Request Overview
This PR updates the type signature of the groupBy function to more accurately reflect that not all keys of type K will necessarily be present in the returned object. The return type is changed from Record<K, V[]> to Partial<Record<K, V[]>>, indicating that keys may be undefined.
Key changes:
- Modified
groupByfunction signature to returnPartial<Record<K, V[]>>instead ofRecord<K, V[]> - Added null/undefined checks and non-null assertions across the codebase where
groupByresults are accessed
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/vs/base/common/collections.ts | Updated groupBy return type to Partial<Record<K, V[]>> |
| src/vs/base/test/common/collections.test.ts | Added optional chaining when accessing grouped results in tests |
| src/vs/workbench/contrib/scm/browser/scmHistoryViewPane.ts | Added null checks for historyItemRefs before and after grouping |
| src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModelImpl.ts | Added non-null assertion when accessing grouped result |
| src/vs/workbench/contrib/mcp/browser/mcpCommands.ts | Added non-null assertions when accessing and iterating grouped servers |
| src/vs/workbench/contrib/markers/test/browser/markersModel.test.ts | Added null check before accessing grouped markers |
| src/vs/workbench/contrib/issue/browser/baseIssueReporterService.ts | Added nullish coalescing for themes array and default empty array |
| src/vs/workbench/contrib/chat/browser/modelPicker/modePickerActionItem.ts | Added optional chaining and nullish coalescing for builtin modes |
| src/vs/base/common/lifecycle.ts | Added null check when iterating over continuations |
| if (!historyItemRefs) { | ||
| continue; | ||
| } |
There was a problem hiding this comment.
This null check appears redundant. The variable historyItemRefs comes from iterating over historyItemGroups (line 503) which should already be filtered or validated. If this check is necessary, it suggests the source collection may contain invalid data that should be handled earlier in the data flow.
See #272263, it currently assumes that all keys of K will be defined. This still isn't perfect, because it says that the returned object might contain a key with value
undefined, but in reality, if a key is defined, it will have a value. So now usages of this with Object.entries and so on are slighly wrong, but that's preferable IMO.