Better types for Dexie.js
npm install dexie-better-typesThe main improvement is in the type safety of where, orderBy and similar conditions,
which now require that the keys and values passed match the containing type of the Table
First, install the types
``bash`
npm install --save-dev dexie-better-types
Then, use the Table type coming from dexie-better-types, rather than dexie itself.
`typescript
import Dexie from 'dexie';
import type { Table } from 'dexie-better-types'
class MyAppDatabase extends Dexie {
// Declare implicit table properties.
// (just to inform Typescript. Instantiated by Dexie in stores() method)
contacts!: Table
//...other tables goes here...
constructor () {
super("MyAppDatabase");
this.version(1).stores({
contacts: '++id, first, last',
//...other tables goes here...
});
}
}
interface IContact {
id?: number,
first: string,
last: string
}
`
Trying to use the wrong key:
`typescript`
let res = await db.contacts.where('someKey').equals('someValue').toArray()
gets you an error message like the following
`typescript`
No overload matches this call.
Overload 1 of 2, '(index: KeysOf
Argument of type '"someKey"' is not assignable to parameter of type 'KeysOf
Overload 2 of 2, '(equalityCriterias: Partial
Type '"someKey"' has no properties in common with type 'Partial
The checks work for values too, i.e.
`typescript`
let res = await db.contacts.where('first').equals(1).toArray()
yields the error
``
Argument of type 'number' is not assignable to parameter of type 'string'.ts(2345)
Compound keys up to 5 items are supported
`typescript``
// Same error as above
let res = await db.contacts.where(['first', 'last']).equals(['v1', 2]).toArray()