isNotNull TypeScript automatic type Narrowing
Hey guys! I'm having some trouble with the typing of my DB.
Basically I have this select that gets reservations from my DB. The reserveTotal is nullable, but I added in the where the condition isNotNull to it. The problem is that typescript is not recognizing that the reservationTotal is not null and I'm being forced to make a type assertion:
Is there a way to do this without using type assertion? If not, is there a better way to do this?
4 Replies
Also, this get's worse when I need to make a more complex query, like this:
There is no way for TypeScript to know that
isNotNull
makes the result non-nullable. The return-type of isNotNull()
is just SQL
. The SQL
type does not tell TS anything about what you're requesting.
So regardless of what conditions you use, the return type is based solely on which columns were requested and the types of those columns defined by the schema.
A solution would be define the expected column type like this:
With RQB, you can use custom fields to do the same
https://orm.drizzle.team/docs/rqb#include-custom-fields
Drizzle ORM - Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Thanks!! I ended up finding this solution as well. I'll check it out the link you sent.