MySqlInsertValue<> vs typeof Table type mismatch
I've got this line of code:
Where
props
is showing a TS problem:
The variables in there are typed like:
The ModelMap
is an index of all the different types generated by various drizzle schemas I have, so that they are available for quick reference by just passing T
.
ModelMap[T]['model']
is equivalent to typeof SomeTable
and ModelMap[T]['select']
is equivalent to InferModel<typeof SomeTable>
What might be causing this problem?16 Replies
For completeness here's some extra info.
a table:
The resulting
UserModel
the resulting UserSelect
model:
You are trying to pass the selection model into the insert function. You need to pass the insertion model.
It makes no difference, basically the same error
Also, the selection model is the complete version of the insert model, meaning that it has everything thus it should work anyway
Here's the error with the insert model used:
Seems like you have not a single type in your
ModelMap[T]['insert']
, but a union, as indicated by the inferred type here. So there're potentially multiple unrelated types there (probably from different tables, if your ModelMap
type is declared incorrectly?), which you are trying to pass as an insertion value for a specific table. Hence the error.In any case, it's impossible to debug this error without looking at how your types are defined and what they are resolving into. So if you could put the complete code somewhere for me to look, I can provide you more insights on what might be wrong. For now, it seems like the issue is with how you defined your types.
yeah, i said there was a union in there, as that's the point of the type map. but the T disambiguates it.
i'll work on breaking this into a simpler example so you don't need to read through so much code
i should have done that from the start
i'm sorry
I'm fine reading through that code, it's just the samples you've sent doesn't help with identifying the issue
I need to look at how you're building and using the ModelMap type
Gist
demonstrates a problem I'm having passing drizzle props to a generi...
demonstrates a problem I'm having passing drizzle props to a generic doit() function. the T should disambiguate, but it doesn't - drizzle-model-map.ts
this gist simplifies down the problem
^ you get that problem in vs code
OK, I figured it out. Your thinking is correct, it's just that TypeScript is not that clever to resolve this logic on its own. In your example, the
table
and props
arguments are inferred independently from each other, so TypeScript can't verify that they belong to the same table and thus can be used together. I've rewritten your types a bit so that TS can understand what you're trying to do.
https://gist.github.com/dankochetov/0ecf316dfed098a793deef69637361f1Gist
demonstrates a problem I'm having passing drizzle props to a generi...
demonstrates a problem I'm having passing drizzle props to a generic doit() function. the T should disambiguate, but it doesn't - drizzle-model-map.ts
It may seem like it does the exact same thing, but it works differently under the hood. Honestly, if you ask me why, I couldn't answer - I just tried it and it worked. š¤·āāļø
heh. typescript is so damn weird!
thank you so much
np
holy cow that breaks my code so much...i guess i've got a lot of ts debugging to work out. not your problem, but thanks for solving this one.
just ran into another weird TS quirk.
it thinks this is bad:
but this is good:
They are effectively the same thing TS!
š