Skip to content

Commit 0f4c1ab

Browse files
committed
Add: NullDriver return value type is never
1 parent 380bacc commit 0f4c1ab

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Changelog
22

3-
## 0.4.1 (DEV)
3+
## 0.5.0 (2023-12-16)
44

5+
- Add: A 3rd optional generic parameter `VOut` to all of `StoreDriver`
6+
interfaces to declare what can be returned.
7+
- Upd: `createNullDriver` now returns driver with `VOut` set to `never` for
8+
better types inference.
59
- Fix: `createLocalStorageDriver` didn't use `serialize` and `unserialize`.
610

711
## 0.4.0 (2023-12-06)

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ npm i @cubux/storage-driver
1414

1515
## API
1616

17-
### `interface StoreDriverSingle<K, V>`
17+
### `interface StoreDriverSingle<K, V, VOut = V>`
1818

1919
Driver interface to read/write data by single item.
2020

2121
#### Methods
2222

23-
##### `getItem(key: K): Promise<V | undefined>`
23+
##### `getItem(key: K): Promise<VOut | undefined>`
2424

2525
Get value of specific item with the given key.
2626

@@ -32,13 +32,13 @@ Remove item with the given key.
3232

3333
Store the given value in item with the given key.
3434

35-
### `interface StoreDriverMapped<K, V>`
35+
### `interface StoreDriverMapped<K, V, VOut = V>`
3636

3737
Driver interface to read/write all items at once.
3838

3939
#### Methods
4040

41-
##### `getAll(): Promise<ReadonlyMap<K, V>>`
41+
##### `getAll(): Promise<ReadonlyMap<K, VOut>>`
4242

4343
Get all items.
4444

@@ -47,7 +47,7 @@ Get all items.
4747
Replace all stored items with the given new items. That is keys became missing
4848
will be removed from storage.
4949

50-
### `interface StoreDriver<K, V>`
50+
### `interface StoreDriver<K, V, VOut = V>`
5151

5252
Generalized interface covering all specific interfaces above.
5353

@@ -101,5 +101,5 @@ actual driver. This driver always contains nothing in reads and does nothing on
101101
writes.
102102
103103
```ts
104-
function createNullDriver<K = any, V = any>(): StoreDriver<K, V>
104+
function createNullDriver<K = any, V = any>(): StoreDriver<K, V, never>
105105
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cubux/storage-driver",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "Simple storage driver abstraction.",
55
"keywords": [
66
"storage",

src/null.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ interface CreateNullDriverFn {
1111
* omit actual driver. This driver always contains nothing in reads and does
1212
* nothing on writes.
1313
*/
14-
<K, V>(): StoreDriver<K, V>;
14+
<K, V>(): StoreDriver<K, V, never>;
1515

1616
/**
1717
* Create dummy no-op driver. Can be used for example in test environment to
1818
* omit actual driver. This driver always contains nothing in reads and does
1919
* nothing on writes.
2020
*/
21-
<K>(): StoreDriver<K, any>;
21+
<K>(): StoreDriver<K, any, never>;
2222

2323
/**
2424
* Create dummy no-op driver. Can be used for example in test environment to
2525
* omit actual driver. This driver always contains nothing in reads and does
2626
* nothing on writes.
2727
*/
28-
(): StoreDriver<any, any>;
28+
(): StoreDriver<any, any, never>;
2929
}
3030

3131
/**
@@ -35,7 +35,7 @@ interface CreateNullDriverFn {
3535
*/
3636
export const createNullDriver: CreateNullDriverFn = () => new NullDriver();
3737

38-
class NullDriver implements StoreDriver<any, any> {
38+
class NullDriver implements StoreDriver<any, any, never> {
3939
getItem(key: any) {
4040
return Promise.resolve(undefined);
4141
}
@@ -46,7 +46,7 @@ class NullDriver implements StoreDriver<any, any> {
4646
return Promise.resolve();
4747
}
4848
getAll() {
49-
return Promise.resolve(new Map());
49+
return Promise.resolve(new Map<any, never>());
5050
}
5151
setAll(items: ReadonlyMap<any, any>) {
5252
return Promise.resolve();

src/types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
22
* Driver interface to read/write data by single item.
33
*/
4-
export interface StoreDriverSingle<K, V> {
4+
export interface StoreDriverSingle<K, V, VOut extends V = V> {
55
/**
66
* Get value of specific item with the given key.
77
*/
8-
getItem(key: K): Promise<V | undefined>;
8+
getItem(key: K): Promise<VOut | undefined>;
99
/**
1010
* Remove item with the given key.
1111
*/
@@ -19,11 +19,11 @@ export interface StoreDriverSingle<K, V> {
1919
/**
2020
* Driver interface to read/write all items at once.
2121
*/
22-
export interface StoreDriverMapped<K, V> {
22+
export interface StoreDriverMapped<K, V, VOut extends V = V> {
2323
/**
2424
* Get all items.
2525
*/
26-
getAll(): Promise<ReadonlyMap<K, V>>;
26+
getAll(): Promise<ReadonlyMap<K, VOut>>;
2727
/**
2828
* Replace all stored items with the given new items. That is keys became
2929
* missing will be removed from storage.
@@ -34,6 +34,6 @@ export interface StoreDriverMapped<K, V> {
3434
/**
3535
* Generalized interface covering all specific interfaces.
3636
*/
37-
export interface StoreDriver<K, V>
38-
extends StoreDriverSingle<K, V>,
39-
StoreDriverMapped<K, V> {}
37+
export interface StoreDriver<K, V, VOut extends V = V>
38+
extends StoreDriverSingle<K, V, VOut>,
39+
StoreDriverMapped<K, V, VOut> {}

0 commit comments

Comments
 (0)