Database Type Parameter
Type safety starts with the database type parameter passed toKysely<DB>:
Column Selection Type Inference
Kysely infers the return type based on selected columns:Aliased Columns
SelectAll
Table Reference Validation
Only tables defined in the database interface can be referenced:Column Reference Validation
Columns must exist in the selected table(s):JOIN Type Safety
After a join, columns from both tables are available:Join Reference Validation
WHERE Clause Type Safety
WHERE clauses are validated against available columns and their types:INSERT Type Safety
Inserted values must match the table’sInsertable type:
UPDATE Type Safety
Updated values must match the table’sUpdateable type:
Return Type Inference
Return types change based on execution method:Expression Builder Type Safety
The expression builder provides type-safe access to columns in callbacks:eb parameter knows which columns are available in the current context:
Type Narrowing with Generics
Kysely’s query builders are generic, allowing for composable, type-safe functions:Dynamic Table Access
For runtime-dynamic table access, usedb.dynamic.ref():
Compile-Time vs Runtime
Kysely provides compile-time type safety. Type information is erased at runtime:Best Practices
Kysely’s type safety is structural, not nominal. Two tables with identical column types are considered compatible.