Skip to content

pgtyped on more runtimes: avoid relying on global Buffer type #612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SebastienGllmt
Copy link
Contributor

@SebastienGllmt SebastienGllmt commented Apr 29, 2025

Buffer is not a global type inherent to Typescript (comes from nodejs) so not all compiler options support it. Therefore, using pgtyped in those environments can lead to a compiler error on the Buffer type

Tools usually solve this with two options:

  1. Mock out the node:buffer module (so import type { Buffer } from "node:buffer"; works)
  2. Add Buffer to the global namespace

Adding to the global namespace is generally bad, so option (1) is usually preferred. However, pgtyped only supported option (2)

This PR changes so files generated from pgtyped include import type { Buffer } from 'node:buffer'; in the generated code so that projects using pgtyped don't have to pollute their global namespace to get the project working. This reduces the need for hacks like #262 (comment)

Copy link

vercel bot commented Apr 29, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
pgtyped ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 29, 2025 9:14am

@@ -24,7 +24,7 @@ const DateOrString: Type = {
name: 'DateOrString',
definition: 'Date | string',
};
const Bytes: Type = { name: 'Buffer' };
const Bytes: Type = { name: 'Buffer', from: 'node:buffer' };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fortunately, this whole PR is just a single line (everything else is tests)

@SebastienGllmt SebastienGllmt mentioned this pull request May 1, 2025
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.

1 participant