However, there's no guarantees that the provided subquery will actually return any rows, and in the case where there are no rows returned, some_id would evaluate to null.
Is there a way to get string | null as the type for some_id (without using $castTo)? If not, I think it would make sense to either make these values nullable by default, or provide a way to configure this behavior either globally or an a per-query basis.
If the value was nullable by default, that would obviously be a pretty significant breaking change. However, it is arguably the safest approach, since the nullability of the value is contingent on not just the WHERE condition inside the subquery, but also the state of the database itself. The value could safely be made non-nullable by wrapping the whole expression inside a coalesce function. If there's a particular scenario where the value could never be null, then arguably that would be an appropriate use case for manually overriding the type -- and not the other way around. Just my two cents.
Example from the playground:
Here, the resulting type is
However, there's no guarantees that the provided subquery will actually return any rows, and in the case where there are no rows returned,
some_id
would evaluate tonull
.Is there a way to get
string | null
as the type forsome_id
(without using$castTo
)? If not, I think it would make sense to either make these values nullable by default, or provide a way to configure this behavior either globally or an a per-query basis.If the value was nullable by default, that would obviously be a pretty significant breaking change. However, it is arguably the safest approach, since the nullability of the value is contingent on not just the WHERE condition inside the subquery, but also the state of the database itself. The value could safely be made non-nullable by wrapping the whole expression inside a
coalesce
function. If there's a particular scenario where the value could never be null, then arguably that would be an appropriate use case for manually overriding the type -- and not the other way around. Just my two cents.