Module

x/kysely_postgrs_js_dialect/deps.ts>kysely.UpdateQueryBuilder#$assertType

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

Asserts that query's output row type equals the given type T.

This method can be used to simplify excessively complex types to make typescript happy and much faster.

Kysely uses complex type magic to achieve its type safety. This complexity is sometimes too much for typescript and you get errors like this:

error TS2589: Type instantiation is excessively deep and possibly infinite.

In these case you can often use this method to help typescript a little bit. When you use this method to assert the output type of a query, Kysely can drop the complex output type that consists of multiple nested helper types and replace it with the simple asserted type.

Using this method doesn't reduce type safety at all. You have to pass in a type that is structurally equal to the current type.

Examples

const result = await db
  .with('updated_person', (qb) => qb
    .updateTable('person')
    .set(person)
    .where('id', '=', person.id)
    .returning('first_name')
    .$assertType<{ first_name: string }>()
  )
  .with('updated_pet', (qb) => qb
    .updateTable('pet')
    .set(pet)
    .where('owner_id', '=', person.id)
    .returning(['name as pet_name', 'species'])
    .$assertType<{ pet_name: string, species: Species }>()
  )
  .selectFrom(['updated_person', 'updated_pet'])
  .selectAll()
  .executeTakeFirstOrThrow()

Type Parameters

T extends O

Returns

O extends T ? UpdateQueryBuilder<DB, UT, TB, T> : KyselyTypeError<`$assertType() call failed: The type passed in is not equal to the output type of the query.`>