Skip to content

Commit

Permalink
TInsertType in table() methods on Dexie and Transaction (#1998)
Browse files Browse the repository at this point in the history
* TInsertType in table() methods on Dexie and Transaction

* Testing that typings of table-props works also on transactions.
  • Loading branch information
dfahlander authored May 24, 2024
1 parent 1430bd1 commit 06b9b91
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/public/types/dexie.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export interface Dexie {

open(): PromiseExtended<Dexie>;

table<T = any, TKey = IndexableType>(tableName: string): Table<T, TKey>;
table<T = any, TKey = IndexableType, TInsertType=T>(tableName: string): Table<T, TKey, TInsertType>;

transaction<U>(
mode: TransactionMode,
Expand Down
1 change: 1 addition & 0 deletions src/public/types/transaction.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export interface Transaction {
table(tableName: string): Table<any, any>;
table<T>(tableName: string): Table<T, any>;
table<T, Key>(tableName: string): Table<T, Key>;
table<T, Key, TInsertType>(tableName: string): Table<T, Key, TInsertType>;
}
22 changes: 21 additions & 1 deletion test/typings-test/test-typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* It tests Dexie.d.ts.
*/

import Dexie, { IndexableType, Table, replacePrefix } from '../../dist/dexie'; // Imports the source Dexie.d.ts file
import Dexie, { EntityTable, IndexableType, Table, replacePrefix } from '../../dist/dexie'; // Imports the source Dexie.d.ts file
import './test-extend-dexie';
import './test-updatespec';

Expand Down Expand Up @@ -275,3 +275,23 @@ import './test-updatespec';

db.friends.where({name: 'Kalle'}).modify({name: replacePrefix('K', 'C')});
}


{
// Typings for tables in both Dexie and Transaction
interface Friend {
id: number;
name: string;
age: number;
}

const db = new Dexie('dbname') as Dexie & {
friends: EntityTable<Friend, 'id'>;
items: Table<{id: number, name: string}, number>;
}

db.friends.add({name: "Foo", age: 22});
db.transaction('rw', db.friends, tx => {
tx.friends.add({name: "Bar", age: 33});
})
}

0 comments on commit 06b9b91

Please sign in to comment.