Closed akhilpai closed 4 years ago
Can you give me a full reduced code sample?
Sure, try removing the cast at: https://github.com/akhilpai/idb-153/blob/master/src/example.ts
I did not expect the error:
Argument of type '"some tag"' is not assignable to parameter of type 'string[] | IDBKeyRange | undefined'.
Just to keep things in the thread:
import { DBSchema, openDB } from "idb";
interface Schema extends DBSchema {
entries: {
key: string;
value: { id: string; tags: string[] };
indexes: { tags: string[] };
};
}
(async () => {
let db = await openDB<Schema>("entryDB", 1, {
async upgrade(db) {
const entryStore = db.createObjectStore("entries", { keyPath: "id" });
entryStore.createIndex("tags", "tags", {
multiEntry: true
});
}
});
await db.add("entries", { id: "some id", tags: ["some tag"] });
let result = await db.getAllFromIndex(
"entries",
"tags",
("some tag" as unknown) as string[]
);
console.log(result);
})();
Shouldn't your index type string
rather than string[]
?
interface Schema extends DBSchema {
entries: {
key: string;
value: { id: string; tags: string[] };
indexes: { tags: string }; // <- this line is changed
};
}
Yes, you are right. Thank you for investigating and sorry for the noise!
No worries! Glad folks are finding the typings useful.
Hello, it seems multiEntry index typings are incorrect.
For example, given a schema:
entries: { key: string value: { tags: string[] } indexes }
created as:
const store = db.createObjectStore("entries")
store.createIndex("tags", "tags", { multiEntry: true })
Retrieval by
await db.getFromIndex("entries", "tags", ["some tag"])
returns an empty arraywhile
await db.getFromIndex("entries", "tags", "some tag")
fails with a cast error.and
await db.getFromIndex("entries", "tags", ("some tag" as unknown) as string[])
returns desired data.