isNotNull TypeScript automatic type Narrowing

Hey guys! I'm having some trouble with the typing of my DB.

    const reservations = await db
      .select({
        id: reservationsTable.id,
        _id: reservationsTable._id,
        reserveTotal: reservationsTable.reserveTotal,
        checkInDate: reservationsTable.checkInDate,
        checkOutDate: reservationsTable.checkOutDate,
        partnerName: reservationsTable.partnerName,
      })
      .from(reservationsTable)
      .where(
        and(
          eq(reservationsTable.listingId, listing.id),
          eq(reservationsTable.type, "booked"),
          isNotNull(reservationsTable.reserveTotal), <--
        ),
      );


Basically I have this select that gets reservations from my DB. The reserveTotal is nullable, but I added in the where the condition isNotNull to it. The problem is that typescript is not recognizing that the reservationTotal is not null and I'm being forced to make a type assertion:

   const reservations = (await db
      .select({
        id: reservationsTable.id,
        _id: reservationsTable._id,
        reserveTotal: reservationsTable.reserveTotal,
        checkInDate: reservationsTable.checkInDate,
        checkOutDate: reservationsTable.checkOutDate,
        partnerName: reservationsTable.partnerName,
      })
      .from(reservationsTable)
      .where(
        and(
          eq(reservationsTable.listingId, listing.id),
          eq(reservationsTable.type, "booked"),
          isNotNull(reservationsTable.reserveTotal),
        ),
      )) as Array<
      Pick<
        Reservation,
        "id" | "_id" | "checkInDate" | "checkOutDate" | "partnerName"
      > & {
        reserveTotal: number; // Override to make it non-null since we filter with isNotNull
      }
    >;


Is there a way to do this without using type assertion? If not, is there a better way to do this?
Was this page helpful?