Type Object.groupBy with the ES2024 library
Add ES2024 declarations for Object.groupBy without changing TypeScript's emit target, then handle missing groups and check support in your runtime.
You call Object.groupBy in a Node project, and the editor reports that groupBy does not exist on ObjectConstructor. The Node process you deploy can have the API while your TypeScript config describes an older standard library. Keep the emit target and add the built-in declarations the runtime supports.
Set lib without changing target
Put this in a scratch project's tsconfig.json with TypeScript installed and "type": "module" in package.json. The ES2024 library includes Object.groupBy. The target still controls JavaScript syntax emit.
{
"compilerOptions": {
"target": "ES2022",
"lib": ["ES2024"],
"strict": true,
"types": [],
"outDir": "dist"
},
"include": ["queues.ts"]
}TypeScript now sees the ES2024 built-ins even though you kept ES2022 as the target.
Add queues.ts beside the config. Give each ticket a queue name, then group tickets by that name.
type Queue = 'open' | 'closed'
const tickets: { queue: Queue; id: number }[] = [
{ queue: 'open', id: 42 },
{ queue: 'open', id: 43 },
]
const queues = Object.groupBy(tickets, ticket => ticket.queue)
export const openCount = (queues.open ?? []).length
export const closedCount = (queues.closed ?? []).lengthRun pnpm exec tsc. In Node 22, run node --input-type=module -e "import { openCount, closedCount } from './dist/queues.js'; console.log(openCount, closedCount)" to see 2 0.
The input contains no closed tickets, so queues.closed is undefined. The fallback handles that case even though closed is a valid queue name. If you delete the lib line and run the compiler again, you can reproduce the original missing-property error with this target.
Check the API where the code runs
The declarations let the checker accept the call. The emitted JavaScript still calls Object.groupBy, so run the built code on the Node version your deployment uses. If that process reports Object.groupBy is not a function, upgrade the runtime, load a polyfill before the call, or use a grouping loop.
An explicit lib array needs to list the other environments your project uses. For a browser project, keep DOM if your code uses document. For Node globals, install @types/node and add "node" if your config has an explicit types array.
Give Set methods a separate check. A Node version can offer Set.prototype.intersection while lib: ["ES2024"] still leaves it out of the type definitions. TypeScript 5.7 put those declarations in its ESNext collection library. Before adding a library to fix an editor error, check the declaration in your compiler and run the matching feature on the runtime you deploy.