IDBObjectStore.index resolve to type never
Author: mkaivsCreated Jun 1, 2025Updated Jun 1, 2025
I write a generic function to get the key of a record in an object store, but I keep getting error related to type inference.
export const ObjectStoreName = {
SummaryData: 'SummaryData',
TestData: 'TestData', // DUMMY DATA TO TEST GENERICS
} as const;
export type ObjectStoreName =
(typeof ObjectStoreName)[keyof typeof ObjectStoreName];
export const ObjectStoreIndex = {
ByAssetID: 'ByAssetID',
ByAssetIDAndBatchTime: 'ByAssetIDAndBatchTime',
ByAssetIDAndCreatedAtTimestamp: 'ByAssetIDAndCreatedAtTimestamp',
ByCreatedAtTimestamp: 'ByCreatedAtTimestamp',
ByDummyIndex: 'ByDummyIndex', // DUMMY DATA TO TEST GENERICS
} as const;
export type ObjectStoreIndex =
(typeof ObjectStoreIndex)[keyof typeof ObjectStoreIndex];
interface DataStoreSchema extends DBSchema {
SummaryData: {
key: string;
value: SummaryRecordCache;
indexes: {
ByAssetID: string;
ByAssetIDAndBatchTime: [string, string];
ByAssetIDAndCreatedAtTimestamp: [string, number];
ByCreatedAtTimestamp: number;
};
};
// DUMMY DATA TO TEST GENERICS
TestData: {
key: number;
value: TestRecordCache;
indexes: {
ByDummyIndex: [number, string, number];
};
};
}
async getNextPaginationKey(
storeName: ObjectStoreName,
indexName: ObjectStoreIndex,
lastIndexKey: IDBValidKey | undefined,
assetID: string,
pageSize: number,
): Promise<DatabaseResponse> {
try {
let response: DatabaseResponse = {
status: false,
};
const pagination: typeof response.pagination = {};
let count = 0;
const scannedKeys: IDBValidKey[] = [];
// start transaction
const db = await this._db;
const transaction = db.transaction(storeName, DBAccessMode.Readonly);
const store = transaction.objectStore(storeName);
const storeIndex = store.index(indexName); // error
// ....
}Typescript error:
Argument of type 'ObjectStoreIndex' is not assignable to parameter of type 'never'.
Type '"ByAssetID"' is not assignable to type 'never'.
(parameter) indexName: ObjectStoreIndexTypescript doesn't give me error if I only include SummaryData {....} in DataStoreSchema; after I add TestData{...} to it, I got the above error. I can do store.index(indexName as never) but that doesn't seem safe. How do I deal with the error?
Source: jakearchibald/idb