Incorrect type when using left join and json agg (postgres)
Author: probablykabariCreated May 9, 2024Updated Sep 12, 2026
Labelspostgrestypescript
const result = kysely
.selectFrom("some_table")
.leftJoin("another_table", "some_table.id", "another_table.some_table_id")
.select((eb) => [
eb.fn
.jsonAgg("another_table")
.as("another_records"),
...In the above, the typescript type is incorrect for the result of the json_agg function call because with a left join the values inside the json array can be [ null ] if there are no records in the join. To resolve this you need to add .filterWhere("another_table.id", "is not", null), which would make the result of the agg just null, however this type is also incorrect in the library.
This is just a type issue, the query works as expected.
Source: kysely-org/kysely