fix(expo-db-sqlite-persistence): resolve TS2322 when passing SQLiteDatabase to createExpoSQLitePersistence #1560
main fromrefs/pull/1560/head·opened 🎯 Changes#
The basic usage pattern for createExpoSQLitePersistence produced a TS2322 error:
import * as SQLite from 'expo-sqlite'
import { createExpoSQLitePersistence } from '@tanstack/expo-db-sqlite-persistence'
const database = await SQLite.openDatabaseAsync('database.sqlite')
export const persistence = createExpoSQLitePersistence({
database,
^^^^^^^^
})
TS2322: Type SQLiteDatabase is not assignable to type ExpoSQLiteDatabaseLike
Type SQLiteDatabase is not assignable to type ExpoSQLiteQueryable
Types of property getAllAsync are incompatible.
Type '{ <T>(source: string, params: SQLiteBindParams): Promise<T[]>; <T>(source: string, ...params SQLiteVariadicBindParams): Promise<T[]>; }' is not assignable to type '<T>(sql: string, params?: ExpoSQLiteBindParams | undefined) => Promise<readonly T[]>'.
Types of parameters params and params are incompatible.
Type ExpoSQLiteBindParams | undefined is not assignable to type SQLiteBindParams
Type undefined is not assignable to type SQLiteBindParams
expo.d.ts(7, 5): The expected type comes from property database which is declared here on type ExpoSQLitePersistenceOptions
The root cause was that ExpoSQLiteQueryable defined getAllAsync and runAsync with locally-defined loose types (ReadonlyArray<unknown> | Record<string, unknown>) for bind params. expo-sqlite's SQLiteDatabase defines these methods using strict TypeScript overloads, which TypeScript treats as incompatible with the optional params form using a different type, making SQLiteDatabase unassignable to ExpoSQLiteQueryable.
What changed#
Replaced the locally-defined
ExpoSQLiteBindParamsandExpoSQLiteRunResulttypes in the driver with direct imports ofSQLiteBindParamsandSQLiteRunResultfromexpo-sqlite, makingExpoSQLiteQueryablestructurally compatible with the realSQLiteDatabase.Changed
withExclusiveTransactionAsyncto match expo-sqlite's actual signature which returnsPromise<void>, not a genericPromise<T>. The driver captures the transaction result via a local variable to preserve the return value.Added
normalizeBindValueto validate bind parameter types at the driver boundary, replacing the previous untyped spread. Also addednormalizeRuntimeSqliteParamsin the emulator runtime to explicitly rejectUint8Arrayvalues before they get JSON-serialized as {} over the HTTP bridge.
✅ Checklist#
- I have tested this code locally with
pnpm test.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
Summary by CodeRabbit#
Bug Fixes
- Fixed a TypeScript type error when using Expo SQLite database instances with the persistence layer.
Refactor
- Tightened validation and handling of SQL bind parameters for more predictable behavior.
- Made transaction handling and runtime persistence behavior more consistent and reliable.
- 1
🧹 Nitpick comments (1)
packages/expo-db-sqlite-persistence/e2e/expo-runtime-app/App.tsx (1)
180-183: ⚡ Quick winExtract repeated optional-params dispatch into one helper.
The same
params === undefined ? ... : ...branching is duplicated four times; centralizing it reduces drift risk between db and tx paths.♻️ Proposed refactor
+type CommandParams = Extract<ExpoRuntimeCommand, { params?: unknown }>['params'] + +function callWithOptionalParams<TResult>( + sql: string, + params: CommandParams, + withoutParams: (sql: string) => Promise<TResult>, + withParams: (sql: string, params: NonNullable<CommandParams>) => Promise<TResult>, +): Promise<TResult> { + return params === undefined + ? withoutParams(sql) + : withParams(sql, params) +} @@ - return command.params === undefined - ? database.getAllAsync(command.sql) - : database.getAllAsync(command.sql, command.params) + return callWithOptionalParams( + command.sql, + command.params, + (sql) => database.getAllAsync(sql), + (sql, params) => database.getAllAsync(sql, params), + ) @@ - return command.params === undefined - ? database.runAsync(command.sql) - : database.runAsync(command.sql, command.params) + return callWithOptionalParams( + command.sql, + command.params, + (sql) => database.runAsync(sql), + (sql, params) => database.runAsync(sql, params), + ) @@ - return command.params === undefined - ? transaction.transaction.getAllAsync(command.sql) - : transaction.transaction.getAllAsync(command.sql, command.params) + return callWithOptionalParams( + command.sql, + command.params, + (sql) => transaction.transaction.getAllAsync(sql), + (sql, params) => transaction.transaction.getAllAsync(sql, params), + ) @@ - return command.params === undefined - ? transaction.transaction.runAsync(command.sql) - : transaction.transaction.runAsync(command.sql, command.params) + return callWithOptionalParams( + command.sql, + command.params, + (sql) => transaction.transaction.runAsync(sql), + (sql, params) => transaction.transaction.runAsync(sql, params), + )As per coding guidelines,
Extract common logic into utility functions when identical or near-identical code blocks appear in multiple places.Also applies to: 189-192, 250-253, 259-262
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/expo-db-sqlite-persistence/e2e/expo-runtime-app/App.tsx` around lines 180 - 183, Extract the repeated optional-params branching into a single helper (e.g., getAllWithOptionalParams) that takes an executor object (database or transaction) and the command, and calls executor.getAllAsync(command.sql) when command.params is undefined or executor.getAllAsync(command.sql, command.params) otherwise; replace the four duplicate ternary uses (the blocks referencing command.params and database.getAllAsync) with calls to this helper so both db and tx code paths use the same implementation.🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. Nitpick comments: In `@packages/expo-db-sqlite-persistence/e2e/expo-runtime-app/App.tsx`: - Around line 180-183: Extract the repeated optional-params branching into a single helper (e.g., getAllWithOptionalParams) that takes an executor object (database or transaction) and the command, and calls executor.getAllAsync(command.sql) when command.params is undefined or executor.getAllAsync(command.sql, command.params) otherwise; replace the four duplicate ternary uses (the blocks referencing command.params and database.getAllAsync) with calls to this helper so both db and tx code paths use the same implementation.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
9b45186f-368c-4078-9f7b-1ed5f469dd3b📥 Commits
Reviewing files that changed from the base of the PR and between be656be5c5fa65cc1c2bf76bc0b60be3d9d2a577 and 39f0e75b99419e75d2141cc01006e945b6017b6f.
📒 Files selected for processing (7)
.changeset/six-ties-leave.mdpackages/expo-db-sqlite-persistence/e2e/expo-runtime-app/App.tsxpackages/expo-db-sqlite-persistence/e2e/runtime-protocol.tspackages/expo-db-sqlite-persistence/src/expo-sqlite-driver.tspackages/expo-db-sqlite-persistence/tests/helpers/expo-emulator-database-factory.tspackages/expo-db-sqlite-persistence/tests/helpers/expo-emulator-runtime.tspackages/expo-db-sqlite-persistence/tests/helpers/expo-sqlite-test-db.ts
Closing as superseded by merged PR #1866. That PR incorporated the Expo SQLite boundary repair: compatibility with the installed expo-sqlite overloads, preservation of transaction callback results across the vendor void-return API, and bind-value validation, with type and runtime regression tests.
KyleAMathews(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
e19847b9-b633-44f4-8f49-ac0ce2dd3a2c📥 Commits
Reviewing files that changed from the base of the PR and between 39f0e75b99419e75d2141cc01006e945b6017b6f and 5fe3a0aa62ac13eb82b14ba51b280cc5e1eae5b5.
📒 Files selected for processing (3)
packages/expo-db-sqlite-persistence/e2e/runtime-protocol.tspackages/expo-db-sqlite-persistence/src/expo-sqlite-driver.tspackages/expo-db-sqlite-persistence/tests/helpers/expo-emulator-database-factory.ts🚧 Files skipped from review as they are similar to previous changes (2)
📝 Walkthrough
Walkthrough#
This PR standardizes on
expo-sqlitebind/run types, tightens runtime param validation (rejects unsupported types), switches transaction callbacks to void-only semantics, and propagates these changes across driver, tests, emulator runtime, and the E2E app.Changes#
Expo SQLite Type Unification and Bridge Parameter Normalization
packages/expo-db-sqlite-persistence/e2e/runtime-protocol.tsExpoRuntimeSQLiteBindValueandExpoRuntimeSQLiteBindParams; applies these types todb:runandtx:runcommand variants.packages/expo-db-sqlite-persistence/src/expo-sqlite-driver.tsSQLiteBindParams/SQLiteBindValue/SQLiteRunResultfromexpo-sqlite; addsnormalizeBindValue/normalizeParamsenforcing allowed runtime types; retypeswithExclusiveTransactionAsyncto accept aPromise<void>callback and adaptstransactionWithDriverto capture/return callback results appropriately.packages/expo-db-sqlite-persistence/tests/helpers/expo-emulator-database-factory.ts,packages/expo-db-sqlite-persistence/tests/helpers/expo-sqlite-test-db.tsSQLiteBindParams,SQLiteBindValue, andSQLiteRunResultfromexpo-sqlite; make test helpers conditionally forwardparamsonly when provided; change transaction helper signatures to void-only callbacks.packages/expo-db-sqlite-persistence/tests/helpers/expo-emulator-runtime.tsnormalizeBindValueandnormalizeRuntimeSqliteParamsto validate/convert params before bridge transmission (throws onUint8Array); updatescreateDatabaseand transaction wrappers to send normalized params and use void-only transaction callbacks.packages/expo-db-sqlite-persistence/e2e/expo-runtime-app/App.tsxnormalizeSqliteParams; derivesTransactionHandlefromwithExclusiveTransactionAsynctypes; simplifies command handlers to passcommand.paramsdirectly when present and omit whenundefined; updates transaction handling to void-only semantics..changeset/six-ties-leave.md@tanstack/expo-db-sqlite-persistence-e2e-appand@tanstack/expo-db-sqlite-persistenceand notes a TS2322 fix for passingSQLiteDatabaseintocreateExpoSQLitePersistence.Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~25 minutes
Poem#
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share