Skip to content

Commit

Permalink
Fix typings of at methods
Browse files Browse the repository at this point in the history
  • Loading branch information
keijokapp committed May 28, 2024
1 parent e38beb9 commit 5e0949c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
27 changes: 20 additions & 7 deletions lib/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,28 @@ export default class Database<KeyIn = NativeValue, KeyOut = Buffer, ValIn = Nati
// convenience and backwards compatibility.
/** Create a shallow reference to the database at a specified subspace */
at<CKI, CKO, CVI, CVO>(hasSubspace: GetSubspace<CKI, CKO, CVI, CVO>): Database<CKI, CKO, CVI, CVO>
/** Create a shallow reference to the database at the subspace of another database reference */
// at<CKI = KeyIn, CKO = KeyOut, CVI = ValIn, CVO = ValOut>(prefix: KeyIn | null, keyXf?: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>): Database<CKI, CKO, CVI, CVO>

at(prefix: KeyIn | null): Database<KeyIn, KeyOut, ValIn, ValOut>;
at<CKI, CKO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>): Database<CKI, CKO, ValIn, ValOut>;
at(prefix: KeyIn | null, keyXf?: undefined, valueXf?: undefined): Database<KeyIn, KeyOut, ValIn, ValOut>;
at<CKI, CKO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>, valueXf?: undefined): Database<CKI, CKO, ValIn, ValOut>;
at<CVI, CVO>(prefix: KeyIn | null, keyXf: undefined, valueXf: Transformer<CVI, CVO>): Database<KeyIn, KeyOut, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>, valueXf: Transformer<CVI, CVO>): Database<CKI, CKO, CVI, CVO>;

at<CKI, CKO, CVI, CVO>(prefixOrSubspace: GetSubspace<CKI, CKO, CVI, CVO> | KeyIn | null, keyXf?: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>): Database<CKI, CKO, CVI, CVO> {
at<CKI, CKO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>, valueXf?: undefined):
| Database<KeyIn, KeyOut, ValIn, ValOut>
| Database<CKI, CKO, ValIn, ValOut>;
at<CVI, CVO>(prefix: KeyIn | null, keyXf: undefined, valueXf?: Transformer<CVI, CVO>):
| Database<KeyIn, KeyOut, ValIn, ValOut>
| Database<KeyIn, KeyOut, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO> | undefined, valueXf: Transformer<CVI, CVO>):
| Database<KeyIn, KeyOut, CVI, CVO>
| Database<CKI, CKO, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>):
| Database<CKI, CKO, ValIn, ValOut>
| Database<CKI, CKO, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf?: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>):
| Database<KeyIn, KeyOut, ValIn, ValOut>
| Database<CKI, CKO, ValIn, ValOut>
| Database<KeyIn, KeyOut, CVI, CVO>
| Database<CKI, CKO, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefixOrSubspace: GetSubspace<CKI, CKO, CVI, CVO> | KeyIn | null, keyXf?: Transformer<unknown, unknown>, valueXf?: Transformer<unknown, unknown>) {
if (isGetSubspace(prefixOrSubspace)) return new Database(this._db, prefixOrSubspace.getSubspace())
else return new Database(this._db, this.subspace.at(prefixOrSubspace, keyXf, valueXf))
}
Expand Down
26 changes: 21 additions & 5 deletions lib/subspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,28 @@ export default class Subspace<KeyIn = NativeValue, KeyOut = Buffer, ValIn = Nati
// legit all the variants. Typescript can probably infer using less than this,
// but I honestly don't trust it not to land with any or unknown or something
// in some of the derived types
at(prefix: KeyIn | null): Subspace<KeyIn, KeyOut, ValIn, ValOut>;
at<CKI, CKO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>): Subspace<CKI, CKO, ValIn, ValOut>;
at(prefix: KeyIn | null, keyXf?: undefined, valueXf?: undefined): Subspace<KeyIn, KeyOut, ValIn, ValOut>;
at<CKI, CKO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>, valueXf?: undefined): Subspace<CKI, CKO, ValIn, ValOut>;
at<CVI, CVO>(prefix: KeyIn | null, keyXf: undefined, valueXf: Transformer<CVI, CVO>): Subspace<KeyIn, KeyOut, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf?: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>): Subspace<CKI, CKO, CVI, CVO>;
// ***
at(prefix: KeyIn | null, keyXf: Transformer<any, any> = this.keyXf, valueXf: Transformer<any, any> = this.valueXf) {
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>, valueXf: Transformer<CVI, CVO>): Subspace<CKI, CKO, CVI, CVO>;
at<CKI, CKO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>, valueXf?: undefined):
| Subspace<KeyIn, KeyOut, ValIn, ValOut>
| Subspace<CKI, CKO, ValIn, ValOut>;
at<CVI, CVO>(prefix: KeyIn | null, keyXf: undefined, valueXf?: Transformer<CVI, CVO>):
| Subspace<KeyIn, KeyOut, ValIn, ValOut>
| Subspace<KeyIn, KeyOut, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO> | undefined, valueXf: Transformer<CVI, CVO>):
| Subspace<KeyIn, KeyOut, CVI, CVO>
| Subspace<CKI, CKO, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>):
| Subspace<CKI, CKO, ValIn, ValOut>
| Subspace<CKI, CKO, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf?: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>):
| Subspace<KeyIn, KeyOut, ValIn, ValOut>
| Subspace<CKI, CKO, ValIn, ValOut>
| Subspace<KeyIn, KeyOut, CVI, CVO>
| Subspace<CKI, CKO, CVI, CVO>;
at(prefix: KeyIn | null, keyXf: Transformer<unknown, unknown> = this.keyXf, valueXf: Transformer<unknown, unknown> = this.valueXf) {
const _prefix = prefix == null ? null : this.keyXf.pack(prefix)
return new Subspace(concatPrefix(this.prefix, _prefix), keyXf, valueXf)
}
Expand Down

0 comments on commit 5e0949c

Please sign in to comment.