Skip to content

Type errors using arrays with Bun Postgres #376

@spiffytech

Description

@spiffytech

I'm trying to pass an array into a query. I'm seeing Kysely emit type errors because eb.val() expects a string array.

Is there another way I should be implementing this query? Can kysely-postgres-js do something to fix the type error?

(Using Bun's sql.array() seems to be necessary because I get 'malformed uuid' errors when I just pass the JS array straight to eb.val())

await kysely
  .selectFrom('users')
  .where('user_id', '=', (eb) =>
    eb.fn.any(
      eb.val(sql.array(['01970e95-bf74-7000-a170-48f1dc9f2125'], 'uuid')),
      // This works
      eb.val(sql.array(['01970e95-bf74-7000-a170-48f1dc9f2125'], 'uuid') as unknown as string[]),
    ),
  )
  .selectAll()
  .execute()
Argument of type '(eb: ExpressionBuilder<Database, "users">) => KyselyTypeError<"any(expr) call failed: expr must be an array">' is not assignable to parameter of type 'OperandValueExpressionOrList<Database, "users", "user_id">'.
  Type '(eb: ExpressionBuilder<Database, "users">) => KyselyTypeError<"any(expr) call failed: expr must be an array">' is not assignable to type 'OperandExpressionFactory<Database, "users", string | null>'.
    Type 'KyselyTypeError<"any(expr) call failed: expr must be an array">' is not assignable to type 'OperandExpression<string | null>'.ts(2345)
No overload matches this call.
  Overload 1 of 3, '(expr: StringReference<Database, "users">): KyselyTypeError<"any(expr) call failed: expr must be an array">', gave the following error.
    Argument of type 'ExpressionWrapper<Database, "users", SQLArrayParameter>' is not assignable to parameter of type 'StringReference<Database, "users">'.
  Overload 2 of 3, '(subquery: SelectQueryBuilderExpression<Record<string, string | null>>): ExpressionWrapper<Database, "users", string | null>', gave the following error.
    Argument of type 'ExpressionWrapper<Database, "users", SQLArrayParameter>' is not assignable to parameter of type 'SelectQueryBuilderExpression<Record<string, string | null>>'.
      Property 'isSelectQueryBuilder' is missing in type 'ExpressionWrapper<Database, "users", SQLArrayParameter>' but required in type 'SelectQueryBuilderExpression<Record<string, string | null>>'.
  Overload 3 of 3, '(expr: Expression<readonly (string | null)[]>): ExpressionWrapper<Database, "users", string | null>', gave the following error.
    Argument of type 'ExpressionWrapper<Database, "users", SQLArrayParameter>' is not assignable to parameter of type 'Expression<readonly (string | null)[]>'.
      Types of property 'expressionType' are incompatible.
        Type 'SQLArrayParameter | undefined' is not assignable to type 'readonly (string | null)[] | undefined'.
          Type 'SQLArrayParameter' is missing the following properties from type 'readonly (string | null)[]': length, concat, join, slice, and 26 more.ts(2769)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions