From 0828e65f8845dc4b148c0d3b0697fb589b338239 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Fri, 27 Nov 2020 11:23:06 +0100 Subject: fix static types --- packages/taler-wallet-core/src/util/query.ts | 65 ++++++++++++++-------------- 1 file changed, 32 insertions(+), 33 deletions(-) (limited to 'packages/taler-wallet-core/src/util') diff --git a/packages/taler-wallet-core/src/util/query.ts b/packages/taler-wallet-core/src/util/query.ts index fa0d8beb7..08572fbd2 100644 --- a/packages/taler-wallet-core/src/util/query.ts +++ b/packages/taler-wallet-core/src/util/query.ts @@ -270,13 +270,21 @@ class ResultStream { } } -type StrKey = string & keyof T; - type StoreName = S extends Store ? N : never; type StoreContent = S extends Store ? R : never; type IndexRecord = Ind extends Index ? R : never; -export class TransactionHandle> { +type InferStore = S extends Store ? Store : never; +type InferIndex = Ind extends Index< + infer StN, + infer IndN, + infer KT, + infer RT +> + ? Index + : never; + +export class TransactionHandle> { constructor(private tx: IDBTransaction) {} put( @@ -306,14 +314,9 @@ export class TransactionHandle> { } getIndexed< - StoreName extends StrKey, - IndexName extends string, - S extends IDBValidKey, - T - >( - index: Index, - key: any, - ): Promise { + St extends StoreTypes, + Ind extends Index, string, any, any> + >(index: InferIndex, key: any): Promise | undefined> { const req = this.tx .objectStore(index.storeName) .index(index.indexName) @@ -321,39 +324,37 @@ export class TransactionHandle> { return requestToPromise(req); } - iter, T extends StoreTypes[N]>( - store: Store, + iter>( + store: St, key?: any, - ): ResultStream { + ): ResultStream> { const req = this.tx.objectStore(store.name).openCursor(key); - return new ResultStream(req); + return new ResultStream>(req); } iterIndexed< - StoreName extends StrKey, - IndexName extends string, - S extends IDBValidKey, - T - >(index: Index, key?: any): ResultStream { + St extends InferStore, + Ind extends InferIndex, string, any, any>> + >(index: Ind, key?: any): ResultStream> { const req = this.tx .objectStore(index.storeName) .index(index.indexName) .openCursor(key); - return new ResultStream(req); + return new ResultStream>(req); } - delete, T extends StoreTypes[N]>( - store: Store, + delete( + store: InferStore, key: any, ): Promise { const req = this.tx.objectStore(store.name).delete(key); return requestToPromise(req); } - mutate, T extends StoreTypes[N]>( - store: Store, + mutate( + store: InferStore, key: any, - f: (x: T) => T | undefined, + f: (x: StoreContent) => StoreContent | undefined, ): Promise { const req = this.tx.objectStore(store.name).openCursor(key); return applyMutation(req, f); @@ -583,9 +584,7 @@ export class Database { } async getIndexed>( - index: Ind extends Index - ? Index - : never, + index: InferIndex, key: any, ): Promise | undefined> { const tx = this.db.transaction([index.storeName], "readonly"); @@ -624,16 +623,16 @@ export class Database { return new ResultStream(req); } - iterIndex( - index: Index, + iterIndex>( + index: InferIndex, query?: any, - ): ResultStream { + ): ResultStream> { const tx = this.db.transaction([index.storeName], "readonly"); const req = tx .objectStore(index.storeName) .index(index.indexName) .openCursor(query); - return new ResultStream(req); + return new ResultStream>(req); } async runWithReadTransaction>( -- cgit v1.2.3