11import { getMany } from 'idb-keyval'
22import { addListener , cachedObj , debugLog , dispatch , expire , reactCache , setProperty , verifyEntry } from '../shared'
33
4- type valueTypes = {
5- data : cachedObj [ 'data' ] ,
6- obj : cachedObj & { valid : boolean } ,
4+ type returnTypes < T > = {
5+ data : cachedObj < T > [ 'data' ] ,
6+ obj : cachedObj < T > & { valid : boolean } ,
77}
88
99export type getValue <
10- T extends keyof valueTypes | undefined
11- > = ( T extends keyof valueTypes ? valueTypes [ T ] : valueTypes [ 'data' ] ) | undefined
10+ R extends keyof returnTypes < T > | undefined ,
11+ T extends unknown = unknown
12+ > = ( R extends keyof returnTypes < T > ? returnTypes < T > [ R ] : returnTypes < T > [ 'data' ] ) | undefined
1213
1314export type getReturn <
1415 K extends string | string [ ] ,
15- T extends keyof valueTypes | undefined ,
16+ R extends keyof returnTypes < T > | undefined ,
17+ T extends unknown = unknown ,
1618> = K extends string [ ]
17- ? { [ k in K [ number ] ] : getValue < T > }
18- : getValue < T >
19+ ? { [ k in K [ number ] ] : getValue < R , T > }
20+ : getValue < R , T >
1921
2022export function get <
2123 K extends string | string [ ] ,
24+ T extends unknown = unknown ,
2225> (
2326 cache : reactCache ,
2427 store : Parameters < typeof getMany > [ 1 ] ,
@@ -28,10 +31,11 @@ export function get<
2831 loader ?: ( missingKeys : string [ ] ) => Promise < void > ,
2932 expire ?: expire | undefined ,
3033 returnType ?: undefined ,
31- ) : getReturn < K , undefined > ;
34+ ) : getReturn < K , undefined , T > ;
3235export function get <
3336 K extends string | string [ ] ,
34- T extends keyof valueTypes | undefined ,
37+ R extends keyof returnTypes < T > | undefined ,
38+ T extends unknown = unknown ,
3539> (
3640 cache : reactCache ,
3741 store : Parameters < typeof getMany > [ 1 ] ,
@@ -40,12 +44,13 @@ export function get<
4044 keyOrKeys : K ,
4145 loader ?: ( missingKeys : string [ ] ) => Promise < void > ,
4246 expire ?: expire ,
43- returnType ?: T ,
44- ) : getReturn < K , T > ;
47+ returnType ?: R ,
48+ ) : getReturn < K , R , T > ;
4549
4650export function get <
4751 K extends string | string [ ] ,
48- T extends keyof valueTypes | undefined ,
52+ R extends keyof returnTypes < T > | undefined ,
53+ T extends unknown = unknown ,
4954> (
5055 cache : reactCache ,
5156 store : Parameters < typeof getMany > [ 1 ] ,
@@ -54,13 +59,13 @@ export function get<
5459 keyOrKeys : K ,
5560 loader ?: ( missingKeys : string [ ] ) => Promise < void > ,
5661 expire ?: expire ,
57- returnType ?: T ,
58- ) : getReturn < K , T > {
62+ returnType ?: R ,
63+ ) : getReturn < K , R , T > {
5964 const keys = ( Array . isArray ( keyOrKeys ) ? keyOrKeys : [ keyOrKeys ] ) as string [ ]
6065
6166 addListener ( cache , keys , id , rerender )
6267
63- const values : Record < string , getValue < T > > = { }
68+ const values : Record < string , getValue < R , T > > = { }
6469 const missing : string [ ] = [ ]
6570
6671 keys . forEach ( key => {
@@ -69,9 +74,9 @@ export function get<
6974 missing . push ( key )
7075 }
7176 if ( returnType === 'obj' ) {
72- values [ key ] = ( cache [ key ] . obj ? { ...cache [ key ] . obj , valid } : undefined ) as getValue < T >
77+ values [ key ] = ( cache [ key ] . obj ? { ...cache [ key ] . obj , valid } : undefined ) as getValue < R , T >
7378 } else {
74- values [ key ] = cache [ key ] . obj ?. data as getValue < T >
79+ values [ key ] = cache [ key ] . obj ?. data as getValue < R , T >
7580 }
7681 } )
7782
@@ -119,5 +124,5 @@ export function get<
119124 } )
120125 }
121126
122- return ( Array . isArray ( keyOrKeys ) ? values : values [ keys [ 0 ] ] ) as getReturn < K , T >
127+ return ( Array . isArray ( keyOrKeys ) ? values : values [ keys [ 0 ] ] ) as getReturn < K , R , T >
123128}
0 commit comments