InferModelFromColumns with columns defined with sql``
Let's say I have a select list that looks like this
This works fine, but if I try to use it with
InferModelFromColumns
, like so
export type FullBook = InferModelFromColumns<typeof defaultBookFields>;
I get a TS error - the tags and subjects fields are not Columns, but rather ad hoc SQL results.
I do know how to work around this. I could easily Omit<> those two columns, and then manually append a tabs and subjects property of the right type.
But is there any way to make this work as is?
Absolutely love this library by the way - thank you for the amazing work!8 Replies
I believe
InferModelFromColumns
is an internal type used for db.select()
when the user doesn't pass anything to it. It's not meant to be used in this way.
I would suggest you do:
I'm not sure that'll work. I'm don't have access to something to test.I don't think (afaik) you can evaluate expressions like that, in type space. I got as far as this
but the resulting type has a bunch of
never
types on it. In fact, everything is never except for my tags and subjects, presumably because I never passed a generic type to from
but I'm not sure how to do that, and
doesn't work (even if I remove the ReturnType from Y)You're right. What about this:
Just pass to from whatever you're passing in your query
query IS a runtime value
Ohhhhh I think that would work. Very clever!
Incidentally, any idea why this doesn't work. This feels like it should
But I get bizarre ts errors like
Type 'MySqlColumn<{ name: "id"; tableName: "books"; dataType: "number"; columnType: "MySqlInt"; data: number; driverParam: string | number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, object>' does not satisfy the constraint 'Table<TableConfig<Column<any, object, object>>>'. Type 'MySqlColumn<{ name: "id"; tableName: "books"; dataType: "number"; columnType: "MySqlInt"; data: number; driverParam: string | number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, object>' is missing the following properties from type 'Table<TableConfig<Column<any, object, object>>>': $inferSelect, $inferInsert, [IsDrizzleTable]
What's the purpose of
InferSelection
? Just infer the time of a column?That's a the type helper I'm trying to write to do what's described in the question.
Ie, that's my attempted solution of this
This seems to work. It's not pretty, but it seems to work.
Unfortunately the resulting type is a union, the first member of which has a bunch of nevers. But it looks like the subsequent unions (which intellisense does not reveal) make the end result correct
Got it! I was close with my InferSelection<T>. Here's how you do it
Which yields this, copied from the type's intellisense
Nice work Mr. typescript wizard!
😄