query builder AND returns undefined
as per this tweet https://twitter.com/capajj/status/1776858100131074333
also can anyone explain why would it return undefined?
4 Replies
and
is not generic - it can return undefined if you call and(undefined)
(which you can do and can be convenient for dynamic filters).
Because it's not generic it always returns SQL | undefined
since return type doesn't depend on the input
Making it generic can be feasible but complicated - you would have to check that every sql statement passed in as argument could be undefined. If yes - then it returns SQL | undefined
. If no, meaning at least 1 statement is never undefined
- then it would return SQL
in your case just throw in non-null assertion !
since you know you always have like
statement meaning and
always returns some SQL
https://tsplay.dev/WY5gxW
But here's an example of a wrapper you could do if you really wanna makes this work
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
This will be fixed in the future, you can follow this issue
https://github.com/drizzle-team/drizzle-orm/issues/2120
GitHub
[FEATURE]: Enhancing type accuracy for logical operators · Issue #2...
Describe what you want Currently in variables holding SQL query expressions with logical operators like and() or or() can be inferred as SQL<unknown> | undefined, even when conditions are cle...
Lovely, thanks @Mykhailo