Table factory typescript problem
I have a basic table schema that all tables share (id, timestamps and more) and I'm trying to write an helper function that returns a pgTable with these fields already added. Right now it looks like this:
This is wrong though because
testTable
is not typed correctly and it is missing all the shared fields. How would I go about correcting this?2 Replies
I'm not great with typescript and I really hope someone else might provide a better solution, but I couldn't see any obvious way to shim the extra columns into
TColumnsMap
while using the *TableFn
interface for the wrapper, since it's explicitly inferred from the fields
parameter. I ended up typing my function similar to:
I did try my hand at a generic interface to serve as a type for various "table factory functions" using whatever default columns - and it did work - but it uses a lot of copy & paste from the Drizzle ORM implementation, which kind of scared me away given that Drizzle is still fairly unstable.
I would love to know if there's a more canonical solution to typing these sorts of functions.Yeah, at the end I came up with something exactly like your implementation, it will do for now, thank you!