Skip to content

Type mismatch in transaction callback when manually calling trx.commit(value) #6435

@a-badalyan

Description

@a-badalyan

Explain what kind of behaviour you are getting and how you think it should do

I found a mismatch between runtime behavior and TypeScript typings for transactions when trx.commit(value) is called inside a transaction callback.

Runtime behavior:

const trRes = await knex.transaction(async (trx) => {
  const result = await trx.raw('SELECT 1 + 1 AS result');
  await trx.commit(true);
  return result;
});

// trRes is true (boolean), not `result`

So the transaction resolves with the value passed to commit(...), even if callback later returns something else.

Current typings suggest knex.transaction(...) resolves by callback return T, which is misleading in this pattern.

Also, current typings for trx.commit/trx.rollback do not appear to reflect their actual runtime shape.

Expected:

  • Either typings should better model this behavior where possible, and/or
  • docs should explicitly warn that manual trx.commit(value) inside callback can override callback return value for final transaction result.

Error message

No runtime error; this is a behavioral + typing inconsistency.

Notes for potential fix

  • TypeScript updates in types/index.d.ts via Generics,
  • docs clarification in /docs (transactions guide),
  • and link the corresponding docs PR in the main PR description.

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