Skip to content

fix(transactional-adapters): type tx correctly with exactOptionalPropertyTypes - #702

Open
RClaudiuM wants to merge 3 commits into
Papooch:mainfrom
RClaudiuM:fix/695-drizzle-tx-options-exact-optional
Open

RClaudiuM wants to merge 3 commits into
Papooch:mainfrom
RClaudiuM:fix/695-drizzle-tx-options-exact-optional

Conversation

@RClaudiuM

Copy link
Copy Markdown

Fixes #695

What

With exactOptionalPropertyTypes enabled, TransactionHost#tx is typed as never for the Drizzle, Prisma and Mongoose adapters.

Why

  • Drizzle, Prisma, Mongoose: the adapters build their options type from Parameters<...['transaction']>[1]. That parameter is optional, so the type includes undefined. defaultTxOptions?: Partial<TOptions | undefined> then doesn't match TransactionalAdapter's defaultTxOptions?: Partial<TOptions>, so TTxFromAdapter resolves to never. Wrapping the derived type in NonNullable fixes it. TransactionHost always merges the options into an object ({ ...defaultTxOptions, ...options }), so an adapter never receives undefined.
  • Prisma also has an inferred optionsFactory return type. Because of sqlFlavor ? fn : undefined, the emitted .d.ts contains wrapWithNestedTransaction?: (...) | undefined. That also fails to match the interface under exactOptionalPropertyTypes, even after the options fix. Spelling out the return type as TransactionalAdapterOptions<...> makes the declaration match. Runtime behaviour is the same, because TransactionHost still checks typeof wrapWithNestedTransaction === 'function'.

I checked the other adapters (knex, kysely, typeorm, pg-promise, mongodb) against their built declarations with exactOptionalPropertyTypes. They are not affected.

Tests

I added a *-types.spec.ts to each of the three adapters. Each spec uses the TypeScript compiler API to type-check a short consumer snippet that imports the packages by name, so it runs against the built declarations a user gets. The snippet uses txHost.tx, withTransaction(options, ...) and defaultTxOptions. Each spec checks it with exactOptionalPropertyTypes on and off and expects no errors. The specs need no database. Like the other suites, they expect yarn build to have run first, which CI does.

Before the fix (exactOptionalPropertyTypes: true case):

drizzle-orm: "Property 'select' does not exist on type 'never'."
prisma:      "Property '$queryRaw' does not exist on type 'never'."
             "No overload matches this call. ... not assignable to parameter of type 'never'"
mongoose:    "Property 'inTransaction' does not exist on type 'never'."

After the fix, all six cases pass.

Commands run:

yarn build:release
yarn format:check
yarn lint:check
yarn workspace @nestjs-cls/transactional run test                    # 51 passed
yarn workspace @nestjs-cls/transactional-adapter-mongoose run test   # 8 passed
yarn workspace @nestjs-cls/transactional-adapter-drizzle-orm run test
yarn workspace @nestjs-cls/transactional-adapter-prisma run test
yarn workspace nestjs-cls run test:dual-build

The new type specs and the Drizzle sync (better-sqlite3) suite pass. I couldn't run the Postgres-backed Drizzle and Prisma suites locally because they need Docker. The changes to those packages only affect types.

…ptionalPropertyTypes

The transaction options were derived from the optional second parameter of
`transaction()`, so they included `undefined`. With exactOptionalPropertyTypes
enabled, that made the adapter fail to match TransactionalAdapter and typed
TransactionHost#tx as `never`.

Closes Papooch#695
…alPropertyTypes

Drop `undefined` from the options derived from `$transaction()`, and spell out
the return type of optionsFactory so the emitted declaration does not carry
`wrapWithNestedTransaction?: ... | undefined`. Either one made the adapter fail
to match TransactionalAdapter under exactOptionalPropertyTypes, which typed
TransactionHost#tx as `never`.
…onalPropertyTypes

Drop `undefined` from the options derived from the optional second parameter of
`Connection#transaction()`. Under exactOptionalPropertyTypes it made the adapter
fail to match TransactionalAdapter, which typed TransactionHost#tx as `never`.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

transactional-adapter-drizzle-orm: txHost.tx is typed as never with exactOptionalPropertyTypes

1 participant