Last Updated: 3/10/2026
- Expression
Interface Expression
Expression represents an arbitrary SQL expression with a type.
Most Kysely methods accept instances of Expression and most classes like SelectQueryBuilder and the return value of the sql template tag implement it.
Examples
import { type Expression, sql } from 'kysely' const exp1: Expression< string> = sql< string>`CONCAT('hello', ' ', 'world')` const exp2: Expression<{ first_name: string }> = db. selectFrom('person'). select('first_name')You can implement the Expression interface to create your own type-safe utilities for Kysely.
interface Expression<T> {
get expressionType(): undefined | T;
toOperationNode(): OperationNode;
}
Type Parameters
- T
Hierarchy (View Summary)
- OperationNodeSource
- Expression
Index
Accessors
Methods
Accessors
expressionType
-
get expressionType(): undefined | T
All expressions need to have this getter for complicated type-related reasons. Simply add this getter for your expression and always return
undefinedfrom it:Examples
import { type Expression, type OperationNode, sql } from 'kysely' class SomeExpression< T> implements Expression< T> { get expressionType(): T | undefined { return undefined } toOperationNode(): OperationNode { return sql `some sql here`. toOperationNode() }}The getter is needed to make the expression assignable to another expression only if the types
Tare assignable. Without this property (or some other property that referencesT), you could assingExpressiontoExpression.Returns undefined | T
Methods
toOperationNode
-
toOperationNode(): OperationNode
Creates the OperationNode that describes how to compile this expression into SQL.
Examples
If you are creating a custom expression, it’s often easiest to use the sql template tag to build the node:
import { type Expression, type OperationNode, sql } from 'kysely' class SomeExpression< T> implements Expression< T> { get expressionType(): T | undefined { return undefined } toOperationNode(): OperationNode { return sql `some sql here`. toOperationNode() }}Returns OperationNode
Settings
Member Visibility
On This Page
ExamplesAccessors
Methods