Query fragmentation causes typing to break
16 Replies
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
ok, ... 🙂 solid workaround
in such a case those can be constants 🙂
of course not with conditional
if
statementsUnknown User•3mo ago
Message Not Public
Sign In & Join Server To View
So conditional builder would look like this? is this the best way how to do it?
https://kyse.link/toaW0
Variable types can never become wider in typescript. When you join a table, the new query builder's type is wider (contains the information about the joined table). When you assign it to the old variable, the old variable's type doesn't (and can't) change.
So you need to assign the result of a join to a new variable if you want to keep the new wider type. There's no way around it.
So conditional builder would look like this? is this the best way how to do it?That doesn't work either. The type of the resulting query builder is the same as the original one.
Deduplicate joins | Kysely
When building dynamic queries, you sometimes end up in situations where the same join
There might be a way to widen variable types.
A noop
(arg: unknown): asserts arg is WiderType
function call.
Needs investigation for the generic case.Nope, it just narrows it

A better example

I have an example somewhere in which it is not the case.
where you can pretty much decorate an object with extra properties
An object with more properties is narrower
A more specific interface. Less options fit it
&
narrows
|
widens
That's what happens in my first example as wellyou can also apply a union..
If you apply a union, it will narrow the type with the union. It doesn't union anything to the original type.

asserts x is T
will always just set (typeof x) & T
as the typeSorry, confused the lingo.
Anyway,
This is possible:
https://tsplay.dev/wE9Q3w
It's wider in the sense that, there's a wider query context now.