Allow not() to compose with and()/or() (undefined-safe)#6007
Open
J0su3Code wants to merge 1 commit into
Open
Conversation
`and()` and `or()` return `SQL | undefined`, but `not()` only accepted
`SQLWrapper`. As a result `not(and(...))` and `not(or(...))` raised a
TS2345 type error ("'SQL | undefined' is not assignable to 'SQLWrapper'")
and forced users to cast the inner expression to `SQLWrapper`.
Add an overload so `not()` also accepts `SQLWrapper | undefined` and
returns `SQL | undefined`, passing `undefined` straight through the same
way `and()` and `or()` already ignore undefined conditions. The original
overload still returns `SQL` for a definite condition, so existing call
sites keep their exact return type and no behavior changes for them.
Add type-test coverage for not(and(...)) and not(or(...)) across the pg,
mysql, sqlite, singlestore and geldb dialects.
Fixes drizzle-team#1818
a1399b9 to
4e4c8ed
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1818.
and()andor()are typed to returnSQL | undefined(they dropundefinedconditions and returnundefinedwhen nothing is left).not(), however, only acceptedSQLWrapper, so composing them:failed to type-check with:
and forced a manual
as SQLWrappercast on the inner expression.Change
Add an overload to
not()so it also acceptsSQLWrapper | undefinedand returns
SQL | undefined, passingundefinedstraight through — thesame way
and()/or()already ignore undefined conditions.The original overload is kept, so
not(eq(...))still returnsSQL(not
SQL | undefined). No existing call site changes its inferred type,so this is non-breaking.
Tests
Added type-test coverage for
not(and(...))andnot(or(...))in theallOperatorsblock of the pg, mysql, sqlite, singlestore and geldbtype-tests/*/select.tsfiles. These lines fail to compile onmain(the exact TS2345 above) and compile with this change.
Note on conventions
This is a core, dialect-agnostic operator change, so it doesn't map to a
single
[<dialect>]:tag — the type-tests are updated for every dialectinstead.