drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: isNotNull doesn't correctly infer the type

Open ColeBlender opened this issue 1 year ago • 9 comments

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

ColeBlender avatar Mar 20 '24 01:03 ColeBlender

I have also noticed this behaviour. Would be great to be able to remove some 'as string' that I know are redundant

JohnAllenTech avatar Mar 21 '24 00:03 JohnAllenTech

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.

acontreras89 avatar May 08 '24 10:05 acontreras89

Yeah agreed actually didnt think of that scenario

JohnAllenTech avatar May 08 '24 11:05 JohnAllenTech

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;
}[]

Vasallius avatar Jul 21 '24 06:07 Vasallius

+1!

glassworks-projects avatar Jul 25 '24 16:07 glassworks-projects

+1 same here. Is there any workaround for this?

Briscoooe avatar Aug 07 '24 05:08 Briscoooe

Facing same issue. Do yall have some workarounds?

sayandcode avatar Aug 09 '24 06:08 sayandcode

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!

sayandcode avatar Aug 09 '24 06:08 sayandcode

+1

bornaradinovic avatar Aug 09 '24 11:08 bornaradinovic

Closing in favor of #1826.

L-Mario564 avatar Oct 18 '24 19:10 L-Mario564

@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.

acontreras89 avatar Oct 19 '24 18:10 acontreras89

@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.

My mistake. This issue was actually closed in favor of #2956.

L-Mario564 avatar Oct 19 '24 19:10 L-Mario564