Skip to content

feat: inline types to reduce install size#194

Open
eduardoboucas wants to merge 1 commit intoneondatabase:mainfrom
netlify:eb/inline-types
Open

feat: inline types to reduce install size#194
eduardoboucas wants to merge 1 commit intoneondatabase:mainfrom
netlify:eb/inline-types

Conversation

@eduardoboucas
Copy link
Copy Markdown

@eduardoboucas eduardoboucas commented Mar 17, 2026

Summary

  • Remove @types/node and @types/pg from runtime dependencies by replacing them with local type shims that are inlined at build time
  • The package now has zero runtime dependencies, reducing install size from 3.8 MB to 456 KB

Problem

@types/node (~2.5 MB) and @types/pg (~44 KB) were listed as runtime dependencies, meaning they — along with their transitive dependencies (pg-protocol, pg-types, undici-types, and various postgres-* packages) — were installed by every consumer of this package. In total, 12 packages and 3.8 MB were pulled in, when only 456 KB was the actual package.

Solution

Replace the runtime dependency on @types/node and @types/pg with local type shims that are inlined into the published declaration files at build time.

New files

  • src/shims/pg/index.d.ts — Local declarations for the pg types this package re-exports (ClientConfig, PoolConfig, QueryResult, etc.). These are
    proper .d.ts files, type-checked by the compiler.
  • src/shims/events/index.d.ts — Minimal EventEmitter declaration, replacing the one from @types/node.
  • tests/type-compatibility.ts — Compile-time test that verifies our shims' property keys match the real @types/pg in both directions.

Modified files

  • tsconfig.json — Added paths to resolve pg and events imports to the local shims during type generation. esbuild ignores tsconfig paths, so the real modules are still used for the runtime bundle.
  • api-extractor.json — Added pg and events to bundledPackages so api-extractor inlines the shim types into the published .d.ts files.
  • build.sh — Added a sed step to strip /// <reference types="node" /> from the subtls dependency before api-extractor runs (subtls's upstream .d.ts references node types).
  • package.json — Moved @types/node and @types/pg from dependencies to devDependencies. Added test:type-shims script.
  • src/shims/net/index.ts — Changed write() and end() type signatures from Buffer | string to Uint8Array | string. This is type-only; the runtime implementation is unchanged. Buffer extends Uint8Array, so existing code that passes Buffer values continues to work.

Preventing drift

npm run test:type-shims (wired into npm test) verifies that the property keys on our shims match the real @types/pg types in both directions. If @types/pg adds or removes a property, the build fails.

Impact

Before After
Runtime dependencies 12 packages 0
Install size (node_modules) 3.8 MB 456 KB
Published .d.ts Imports from pg and events Self-contained

@jawj
Copy link
Copy Markdown
Collaborator

jawj commented Mar 18, 2026

@eduardoboucas Thanks — this looks like a great PR. Can I ask if you have an urgent need for this? (I need to spend a bit more time checking it out, and I'm away next week).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants