DT
Join ServerDrizzle Team
help
Raw sql nullable types, sql<Type | undefined>
const result = await db.select({
customField: sql<Type | null>`...`
This gets inferred to
SQL<any>
Is there a way to have a nullable type when using raw sql?
yes, it should use the exact type passed as the generic
could you post your whole query and the definition of
Type
?Sorry, mistook the issue a bit, it seems that without the
Type
But if I remove the
It correctly is inferred to
The
.mapWith()
its inferred properly, but if its mapped then it is always non-nullable. Probably a niche situation but still a bit unusual, here's my whole thingdestination: sql<Point | null>`CASE WHEN driver_shifts.ride_id IS NOT NULL THEN active_rides.destination END`.mapWith(driverShifts.location),
Type
(property) destination: SQL<Point>
But if I remove the
mapWith
It correctly is inferred to
(property) destination: SQL<Point | null>
export const driverShifts = pgTable('driver_shifts', {
id: uuid('id').primaryKey().defaultRandom(),
createdAt: timestamp('created_at').defaultNow().notNull(),
location: pointDB("location").notNull(),
});
The
pointDB
is a custom PGColumn. But I don't think I can pass it to mapWith but rather I have to use a column from the table, this is a seperate question, don't think its related to the above problemOK I see - this is an edge case I didn't think about
We automatically assign the result type when you call
So the fastest fix on your side would be to make a custom mapper function that returns a nullable result, and pass it into
something like
We automatically assign the result type when you call
.mapWith
based on the mapper return typeSo the fastest fix on your side would be to make a custom mapper function that returns a nullable result, and pass it into
mapWith
something like
function mapper(value: any): Point | null {
return driverShifts.location.mapFromDriverValue(value);
}
.mapWith(mapper)
Yeah quite a niche situation, glad I caught it. Thanks @Dan Kochetov
Will you create an issue on GH if this warrants one? I would like to follow it
You can create it yourself if you'd like 🙂