Sub topic 14
Browser, Node.js, and TypeScript Support, Plus a Fallback
This chapter assumes you already know how Object.groupBy and Map.groupBy behave, including the null-prototype result. It also assumes you can read a tsconfig.json file. The goal here is practical: find out whether your runtime has these methods, tell TypeScript about them, and keep your code working where they are missing.
Where the methods are available
Both methods are part of ES2024. Engines shipped them at about the same time, so a runtime that has one almost always has the other.
| Runtime | First version with `Object.groupBy` and `Map.groupBy` |
|---|---|
| Chrome and Edge | 117 |
| Firefox | 119 |
| Safari (macOS and iOS) | 17.4 |
| Node.js | 21.0 |
| Deno | 1.37 |
| Bun | 1.1 |
Node.js 20 does not have the methods. That matters because many servers and CI images still run Node.js 20. Older Safari versions on iPhones that cannot be updated also lack them.
Telling TypeScript about the methods
TypeScript only knows about built-ins that are listed in its lib settings. If the declarations are missing, you get the error "Property 'groupBy' does not exist on type 'ObjectConstructor'". TypeScript 5.4 added the declarations under esnext, and TypeScript 5.7 moved them to es2024. On 5.7 or later, either of these settings works:
{
"compilerOptions": {
"target": "ES2024",
"lib": ["ES2024", "DOM"]
}
}The declared return type of Object.groupBy is Partial<Record<K, T[]>>. Every group is typed as possibly undefined, which matches how the method behaves at runtime: a key that never appeared has no property. Expect to write groups.fruit?.length or a ?? default.
Detecting support and falling back
If you need to support older runtimes, you have two options. You can load a maintained polyfill such as core-js/actual/object/group-by early in your bundle. Or you can write a small local helper that you call directly. The helper below copies the main behavior of Object.groupBy: it passes the element and its index to the callback, accepts any iterable, and returns a null-prototype object.
function groupBy(items, keyFn) {
const groups = Object.create(null);
let index = 0;
for (const item of items) {
const key = keyFn(item, index++);
(groups[key] ??= []).push(item);
}
return groups;
}
const grouped = typeof Object.groupBy === "function"
? Object.groupBy([1, 2, 3], (n) => (n % 2 ? "odd" : "even"))
: groupBy([1, 2, 3], (n) => (n % 2 ? "odd" : "even"));
console.log(grouped.odd); // [1, 3]Writing groups[key] converts the key to a property key, the same way the built-in method does. A Map version follows the same pattern:
function mapGroupBy(items, keyFn) {
const map = new Map();
let index = 0;
for (const item of items) {
const key = keyFn(item, index++);
const group = map.get(key);
if (group) group.push(item);
else map.set(key, [item]);
}
return map;
}
const byParity = (typeof Map.groupBy === "function" ? Map.groupBy : mapGroupBy)(
[1, 2, 3, 4],
(n) => n % 2 === 0
);
console.log(byParity.get(true)); // [2, 4]Exercise
Your project compiles with TypeScript 5.7 using "target": "ES2017", and it deploys to Node.js 20. Object.groupBy fails to type-check, and it would also crash at runtime. What two separate changes fix these problems?
Show answer
For type-checking, add "ES2024" to "lib" (or raise "target" to ES2024) so TypeScript sees the declarations. For the runtime, either upgrade to Node.js 21 or later, or load a polyfill or fallback helper. Changing "lib" only affects types, and Node.js 20 still has no Object.groupBy method.