drizzle-orm
drizzle-orm copied to clipboard
[BUG]: isNotNull doesn't correctly infer the type
What version of drizzle-orm are you using?
0.29.3
What version of drizzle-kit are you using?
0.20.13
Describe the Bug
I have a table called products which has a column called color which is of type text and it can be null.
Here is my code:
const colors = ( await db .selectDistinctOn([products.color], { color: products.color }) .from(products) .where(isNotNull(products.color)) .orderBy(products.color) ).map((product) => product.color);
This is the type of colors:
const colors: (string | null)[]
It should just be:
const colors: string[]
Expected behavior
isNotNull should know that the resulting output will not have any null values
Environment & setup
No response
I have also noticed this behaviour. Would be great to be able to remove some 'as string' that I know are redundant
Would be great to be able to remove some 'as string' ...
More importantly, if someone ever removes the isNotNull condition, either intentionally or by accident, all those casts will hide potential errors.
So this is more about overall DX, not only ergonomics.
Yeah agreed actually didnt think of that scenario
Experiencing the same with this setup
const getUserDashboardSetups = (userUid: string) => {
return db
.select({
dashboardName: dashboards.name,
cardName: cards.name,
dataField: cards.data_field,
isFavorite: isNotNull(favorite_setups.card_id)
})
.from(dashboards)
.innerJoin(dashboard_cards, eq(dashboards.dashboard_id, dashboard_cards.dashboard_id))
.innerJoin(cards, eq(dashboard_cards.card_id, cards.card_id))
.leftJoin(favorite_setups, eq(cards.card_id, favorite_setups.card_id))
.where(eq(dashboards.user_uid, userUid))
.orderBy(dashboards.dashboard_id, dashboard_cards.position);
};
the function's return type is
const userDashboardSetups: {
dashboardName: string;
cardName: string;
dataField: string;
isFavorite: unknown;
}[]
+1!
+1 same here. Is there any workaround for this?
Facing same issue. Do yall have some workarounds?
This is especially a problem when you're using query builder, or something like a union. The option to cast as string or similar doesn't exist, as the type is much more detailed and nuanced. The only practical thing to do is cast as any, which is blasphemy!
+1
Closing in favor of #1826.
@L-Mario564 are you sure this and #1826 are related? as far as I can tell, one is about the usage of isNotNull in select, whereas the other (this one) is about the inferred type when using isNotNull in where clauses.
@L-Mario564 are you sure this and #1826 are related? as far as I can tell, one is about the usage of
isNotNullinselect, whereas the other (this one) is about the inferred type when usingisNotNullinwhereclauses.
My mistake. This issue was actually closed in favor of #2956.