Skip to content

"No overload matches this call" when passing the store name as a variable #319

@retorquere

Description

@retorquere

with the code below, I get

index.ts:93:30 - error TS2769: No overload matches this call.
  Overload 1 of 2, '(storeNames: "ZoteroSerialized" | "ZoteroSerializedTouched" | "ExportCacheContext" | "BetterBibLaTeX" | "BetterBibTeX" | "BetterCSLJSON" | "BetterCSLYAML" | "metadata", mode?: "readwrite" | undefined, options?: IDBTransactionOptions | undefined): IDBPTransaction<...>', gave the following error.
    Argument of type 'string' is not assignable to parameter of type '"ZoteroSerialized" | "ZoteroSerializedTouched" | "ExportCacheContext" | "BetterBibLaTeX" | "BetterBibTeX" | "BetterCSLJSON" | "BetterCSLYAML" | "metadata"'.
  Overload 2 of 2, '(storeNames: ArrayLike<"ZoteroSerialized" | "ZoteroSerializedTouched" | "ExportCacheContext" | "BetterBibLaTeX" | "BetterBibTeX" | "BetterCSLJSON" | "BetterCSLYAML" | "metadata">, mode?: "readwrite" | undefined, options?: IDBTransactionOptions | undefined): IDBPTransaction<...>', gave the following error.
    Argument of type 'string' is not assignable to parameter of type 'ArrayLike<"ZoteroSerialized" | "ZoteroSerializedTouched" | "ExportCacheContext" | "BetterBibLaTeX" | "BetterBibTeX" | "BetterCSLJSON" | "BetterCSLYAML" | "metadata">'.

93   const tx = $db.transaction(name, 'readwrite')
                                ~~~~


index.ts:94:32 - error TS2345: Argument of type 'string' is not assignable to parameter of type '"ZoteroSerialized" | "ZoteroSerializedTouched" | "ExportCacheContext" | "BetterBibLaTeX" | "BetterBibTeX" | "BetterCSLJSON" | "BetterCSLYAML" | "metadata"'.

94   const store = tx.objectStore(name)
                                  ~~~~

index.ts:95:29 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.

95   const index = store.index('itemID')

if I replace name with the constant 'BetterBibTeX' the error goes away

code -- click to show
import { openDB, IDBPDatabase, DBSchema } from 'idb'

export type ExportContext = {
  context: string
  id: number
}

export type ExportCacheContent = {
  context: number
  itemID: number
  entry: string
}

type Serialized = {
  itemID: number
  title: string
}

interface Schema extends DBSchema {
  ZoteroSerialized: {
    value: Serialized
    key: number
  }
  ZoteroSerializedTouched: {
    value: boolean
    key: number
  }

  ExportCacheContext: {
    value: ExportContext
    key: number
    indexes: { context: string }
  }

  BetterBibLaTeX: {
    value: ExportCacheContent
    key: [number, number]
    indexes: { context: number, itemID: number, 'context-itemID': [ number, number ] }
  }
  BetterBibTeX: {
    value: ExportCacheContent
    key: [number, number]
    indexes: { context: number, itemID: number, 'context-itemID': [ number, number ] }
  }
  BetterCSLJSON: {
    value: ExportCacheContent
    key: [number, number]
    indexes: { context: number, itemID: number, 'context-itemID': [ number, number ] }
  }
  BetterCSLYAML: {
    value: ExportCacheContent
    key: [number, number]
    indexes: { context: number, itemID: number, 'context-itemID': [ number, number ] }
  }

  metadata: {
    value: { key: string, value: string | number }
    key: string
  }
}

async function main() {
  const $db = await openDB<Schema>('BetterBibTeXCache', 1, {
    upgrade: (db, oldVersion, newVersion) => {
      if (oldVersion !== newVersion) {
        for (const store of db.objectStoreNames) {
          db.deleteObjectStore(store)
        }
      }

      db.createObjectStore('ZoteroSerialized', { keyPath: 'itemID' })
      db.createObjectStore('ZoteroSerializedTouched')
      db.createObjectStore('metadata')

      const context = db.createObjectStore('ExportCacheContext', { keyPath: 'id', autoIncrement: true })
      context.createIndex('context', 'context', { unique: true })

      const stores = [
        db.createObjectStore('BetterBibTeX', { keyPath: [ 'context', 'itemID' ] }),
        db.createObjectStore('BetterBibLaTeX', { keyPath: [ 'context', 'itemID' ] }),
        db.createObjectStore('BetterCSLJSON', { keyPath: [ 'context', 'itemID' ] }),
        db.createObjectStore('BetterCSLYAML', { keyPath: [ 'context', 'itemID' ] }),
      ]
      for (const store of stores) {
        store.createIndex('context', 'context')
        store.createIndex('itemID', 'itemID')
        store.createIndex('context-itemID', [ 'context', 'itemID' ], { unique: true })
      }
    },
  })

  const name: keyof Schema = 'BetterBibTeX'
  const tx = $db.transaction(name, 'readwrite')
  const store = tx.objectStore(name)
  
  const index = store.index('itemID')
  const ids = [1, 2, 3]
  for (const id of ids) {
    let cursor = await index.openCursor(IDBKeyRange.only(id))
    while (cursor) {
      await store.delete(cursor.primaryKey)
      cursor = await cursor.continue()
    }
  }
  await tx.done
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions