typescriptindexeddb

Typescript: pretents foo is not in type bar, while it is: how to debug


When I do tx.idbtrans, I have an error:

Error: Property 'idbtrans' does not exist on type 'Transaction & { fileInfo: EntityTable<FileInfo, "id">; }'. 
        await db.transaction("rw", x.myUndoTransactionsToApply.tables.flat(), async (tx) => {
          wmNormalUndoOrRedo.set(tx.idbtrans, 2); // 2 means we are in a redo operation
          if(x && x.myUndoTransactionsToApply) 

but if I check the transaction type (either in the documentation or in the source code since I'm using npm link), I can see that idbtrans DOES exist:

$ cat src/classes/transaction/transaction.ts
…
/** Transaction
 * 
 * https://dexie.org/docs/Transaction/Transaction
 * 
 **/
export class Transaction implements ITransaction {
…
  idbtrans: IDBTransaction;
…

Any idea how to debug this? For instance, can I print the full description of Transaction to see what property is supposed to be allowed in Transaction?


Solution

  • If you check the source code on Dexie github, idbtrans does not exist in Transaction's type declaration.

    The last version which idbtrans existed in Transaction was 7 years ago. I think they either forget to update the document, or have messed up the d.ts and not realized.

    You can try manually editing your local node_modules/dexie/dist/dexie.d.ts to add it back and see if this works for you. As you said it probably still exists in the class as suggested by transaction.ts.

    export interface Transaction {
        db: Dexie;
        active: boolean;
        mode: IDBTransactionMode;
        idbtrans: IDBTransaction; //Add this line
        //tables: { [type: string]: Table<any, any> }; Deprecated since 2.0. Obsolete from v3.0.
        storeNames: Array<string>;
        explicit?: boolean;
        parent?: Transaction;
        on: TransactionEvents;
        abort(): void;
        table(tableName: string): Table<any, any>;
        table<T>(tableName: string): Table<T, any>;
        table<T, Key>(tableName: string): Table<T, Key>;
    }
    

    Or by adding these lines in your project's d.ts

    declare module "dexie" {
        interface Transaction{
            idbtrans: IDBTransaction;
        }
    }