Module

x/kysely_postgrs_js_dialect/deps.ts>kysely.DeleteQueryBuilder#$narrowType

Kysely dialect for PostgreSQL using the Postgres.js client.
method kysely.DeleteQueryBuilder.prototype.$narrowType
import { kysely } from "https://dotland.deno.dev/x/kysely_postgrs_js_dialect@v0.27.3/deps.ts";
const { DeleteQueryBuilder } = kysely;

Narrows (parts of) the output type of the query.

Kysely tries to be as type-safe as possible, but in some cases we have to make compromises for better maintainability and compilation performance. At present, Kysely doesn't narrow the output type of the query when using {@link where} and {@link returning} or {@link returningAll}.

This utility method is very useful for these situations, as it removes unncessary runtime assertion/guard code. Its input type is limited to the output type of the query, so you can't add a column that doesn't exist, or change a column's type to something that doesn't exist in its union type.

Examples

Turn this code:

const person = await db.deleteFrom('person')
  .where('id', '=', id)
  .where('nullable_column', 'is not', null)
  .returningAll()
  .executeTakeFirstOrThrow()

if (person.nullable_column) {
  functionThatExpectsPersonWithNonNullValue(person)
}

Into this:

const person = await db.deleteFrom('person')
  .where('id', '=', id)
  .where('nullable_column', 'is not', null)
  .returningAll()
  .$narrowType<{ nullable_column: string }>()
  .executeTakeFirstOrThrow()

functionThatExpectsPersonWithNonNullValue(person)

Returns

DeleteQueryBuilder<DB, TB, NarrowPartial<O, T>>