how to union two queries with json_build_object?
i would like to union two queries to get a result of type
QueryA | QueryB
. this query fails the type checker but the generated query seems to work as intended. is there a type hint i can provide somehow?
https://kyse.link/2TBLFSolution:Jump to solution
The unioned expressions need to have the same type. You used
first_name
in the first one an name
in the second one. If both have the same property name it works. https://kyse.link/Bc300
When you use different keys, the correct type of the JSON object would be { name: string | undefined, first_name: string | undefined }
but Kysely isn't able to infer it....5 Replies
Solution
The unioned expressions need to have the same type. You used
first_name
in the first one an name
in the second one. If both have the same property name it works. https://kyse.link/Bc300
When you use different keys, the correct type of the JSON object would be { name: string | undefined, first_name: string | undefined }
but Kysely isn't able to infer it.alright thanks. so there is no way to make kysely type the whole result to this?
No, that's not possible
This is
by just casting
details
to that typeah ok! 🙇 so this in full https://kyse.link/CtZGS. now i realize what i need is really a discriminated union type though, so something closer to this https://kyse.link/4Zw87. the result gets the correct type now
but then back to
Types of property 'details' are incompatible.
where union
is not happy. is there any way to just convince the union that all is good? 😄 don't mind explicitly casting anything at this point
ok i think i have something https://kyse.link/xpMTf. perhaps not ideal, but works. thank you very much for the help!