jakearchibald / idb

IndexedDB, but with promises
https://www.npmjs.com/package/idb
ISC License
6.2k stars 348 forks source link

egronomic way to provide schema via JSDoc #303

Open randName opened 7 months ago

randName commented 7 months ago

this is more of a personal itch, but as i'm mostly using types via JSDoc there is a small bit of friction when opening the db since there isn't a way to provide generics yet

/** @type {import('idb').IDBPDatabase<import('./schema').MySchema>} */
const db = await openDB('my-database', 1, {
    /* ... */
})

my suggestion would be to provide a prop that would allow openDB to infer the schema

@@ -53,6 +53,10 @@ export interface OpenDBCallbacks<DBTypes extends DBSchema | unknown> {
+  /**
+   * Convenience property for providing the database schema when using JSDoc.
+   */
+  schema?: DBTypes;
 }

so the example above would look like

const db = await openDB('my-database', 1, {
    schema: /** @type {import('./schema').MySchema} */({}),
    /* ... */
})

No runtime code should be affected, but I am unsure if that is a good idea in the first place so feel free to dismiss this if you think it isn't. The workaround isn't too bad and TypeScript might fix it someday™️