TypeScript type of a table that *always* has a numerical "id" column?
Hello. I'm new to TypeScript and Drizzle, and I'm struggling to describe the correct TypeScript type representing a Drizzle Table that always has a numerical "id" column (the primary key essentially).
What I'm trying to do here is to create a generic class parametrized by a table type T that extends from the above constraint. It would feature a
findById method that can safely access the "id" property and return a record of type InferSelectModel<T>.
Roughly:
My efforts have failed so far because I don't quite follow how InferSelectModel works at this point. It seems to iterate over T['_']['columns'], so one would think that TableWithNumericId should be a type where the _ property is the same structure as a Table but also has a hard-coded property "id" of type Column<ColumnBaseConfig<"number", any>>, but I have not been able to achieve this so far. I can tell because TypeScript is telling me "Property 'id' does not exist on type 'TableType'.". Ultimately I do not want to programatically test if the property "id" exists using if {...}, I want this code to work as-is because TypeScript understands that "id" exists, by virtue of InferSelectModel having declared it.
Thank you1 Reply
Check out some of the code I put here - https://discord.com/channels/1043890932593987624/1415789562793558076