adelsz / pgtyped

pgTyped - Typesafe SQL in TypeScript
https://pgtyped.dev
MIT License
2.91k stars 94 forks source link

Better support for `import * from './foo.queries'` #565

Open SebastienGllmt opened 7 months ago

SebastienGllmt commented 7 months ago

Is your feature request related to a problem? Please describe.

Ideally in the index.ts for my project I can just do

export * from './select.queries.js';
export * from './insert.queries.js';
export * from './update.queries.js';

However, this will fail if two or more of these files contain the same type definition. Notably, if for example both contain export type numberArray = (number)[]; then you will get the error

src/index.ts:3:1 - error TS2308: Module './select.queries.js' has already exported a member named 'numberArray'. Consider explicitly re-exporting to resolve the ambiguity.

Describe the solution you'd like

All extra type definitions created by pgtyped such as numberArray and other intermediary types (like enum types) should be places in a single common.ts (or a different name) and shared between all generated files. This doesn't introduce any performance overhead because these are all imported using import type which disappears at compile-time (and shouldn't negatively affect tree shaking either for the same reason).

Workaround

You can update your typesOverrides to alias common types to a common.ts manually:

"typesOverrides": {
    "lobby_status": "@src/common.js#LobbyStatus"
  }

This workaround does come with some unfortunate tradeoffs:

  1. It still requires you manually keep common.ts up-to-date if the SQL enum ever gets changed
  2. It makes that your project build output contains a @src import (see #564 for why)
  3. It won't work for types generated by pgtyped like numberArray that can't be aliased this way.