@@ -27,14 +27,14 @@ export interface RespType<
2727export interface NullReply extends RespType <
2828 RESP_TYPES [ 'NULL' ] ,
2929 null
30- > { }
30+ > { }
3131
3232export interface BooleanReply <
3333 T extends boolean = boolean
3434> extends RespType <
3535 RESP_TYPES [ 'BOOLEAN' ] ,
3636 T
37- > { }
37+ > { }
3838
3939export interface NumberReply <
4040 T extends number = number
@@ -43,7 +43,7 @@ export interface NumberReply<
4343 T ,
4444 `${T } `,
4545 number | string
46- > { }
46+ > { }
4747
4848export interface BigNumberReply <
4949 T extends bigint = bigint
@@ -52,7 +52,7 @@ export interface BigNumberReply<
5252 T ,
5353 number | `${T } `,
5454 bigint | number | string
55- > { }
55+ > { }
5656
5757export interface DoubleReply <
5858 T extends number = number
@@ -61,7 +61,7 @@ export interface DoubleReply<
6161 T ,
6262 `${T } `,
6363 number | string
64- > { }
64+ > { }
6565
6666export interface SimpleStringReply <
6767 T extends string = string
@@ -70,7 +70,7 @@ export interface SimpleStringReply<
7070 T ,
7171 Buffer ,
7272 string | Buffer
73- > { }
73+ > { }
7474
7575export interface BlobStringReply <
7676 T extends string = string
@@ -90,65 +90,65 @@ export interface VerbatimStringReply<
9090 T ,
9191 Buffer | VerbatimString ,
9292 string | Buffer | VerbatimString
93- > { }
93+ > { }
9494
9595export interface SimpleErrorReply extends RespType <
9696 RESP_TYPES [ 'SIMPLE_ERROR' ] ,
9797 SimpleError ,
9898 Buffer
99- > { }
99+ > { }
100100
101101export interface BlobErrorReply extends RespType <
102102 RESP_TYPES [ 'BLOB_ERROR' ] ,
103103 BlobError ,
104104 Buffer
105- > { }
105+ > { }
106106
107107export interface ArrayReply < T > extends RespType <
108108 RESP_TYPES [ 'ARRAY' ] ,
109109 Array < T > ,
110110 never ,
111111 Array < any >
112- > { }
112+ > { }
113113
114114export interface TuplesReply < T extends [ ...Array < unknown > ] > extends RespType <
115115 RESP_TYPES [ 'ARRAY' ] ,
116116 T ,
117117 never ,
118118 Array < any >
119- > { }
119+ > { }
120120
121121export interface SetReply < T > extends RespType <
122122 RESP_TYPES [ 'SET' ] ,
123123 Array < T > ,
124124 Set < T > ,
125125 Array < any > | Set < any >
126- > { }
126+ > { }
127127
128128export interface MapReply < K , V > extends RespType <
129129 RESP_TYPES [ 'MAP' ] ,
130130 { [ key : string ] : V } ,
131131 Map < K , V > | Array < K | V > ,
132132 Map < any , any > | Array < any >
133- > { }
133+ > { }
134134
135135type MapKeyValue = [ key : BlobStringReply | SimpleStringReply , value : unknown ] ;
136136
137137type MapTuples = Array < MapKeyValue > ;
138138
139139type ExtractMapKey < T > = (
140- T extends BlobStringReply < infer S > ? S :
141- T extends SimpleStringReply < infer S > ? S :
142- never
140+ T extends BlobStringReply < infer S > ? S :
141+ T extends SimpleStringReply < infer S > ? S :
142+ never
143143) ;
144144
145145export interface TuplesToMapReply < T extends MapTuples > extends RespType <
146146 RESP_TYPES [ 'MAP' ] ,
147147 {
148- [ P in T [ number ] as ExtractMapKey < P [ 0 ] > ] : P [ 1 ] ;
148+ [ P in T [ number ] as ExtractMapKey < P [ 0 ] > ] : P [ 1 ] ;
149149 } ,
150150 Map < ExtractMapKey < T [ number ] [ 0 ] > , T [ number ] [ 1 ] > | FlattenTuples < T >
151- > { }
151+ > { }
152152
153153type FlattenTuples < T > = (
154154 T extends [ ] ? [ ] :
@@ -193,32 +193,38 @@ type MapKey<
193193 [ RESP_TYPES . BLOB_STRING ] : StringConstructor ;
194194} > ;
195195
196+ type UnwrapConstructor < T > =
197+ T extends StringConstructor ? string :
198+ T extends NumberConstructor ? number :
199+ T extends BooleanConstructor ? boolean :
200+ T extends BigIntConstructor ? bigint :
201+ T ;
196202export type UnwrapReply < REPLY extends RespType < any , any , any , any > > = REPLY [ 'DEFAULT' | 'TYPES' ] ;
197203
198204export type ReplyWithTypeMapping <
199205 REPLY ,
200206 TYPE_MAPPING extends TypeMapping
201207> = (
202- // if REPLY is a type, extract the coresponding type from TYPE_MAPPING or use the default type
203- REPLY extends RespType < infer RESP_TYPE , infer DEFAULT , infer TYPES , unknown > ?
208+ // if REPLY is a type, extract the coresponding type from TYPE_MAPPING or use the default type
209+ REPLY extends RespType < infer RESP_TYPE , infer DEFAULT , infer TYPES , unknown > ?
204210 TYPE_MAPPING [ RESP_TYPE ] extends MappedType < infer T > ?
205- ReplyWithTypeMapping < Extract < DEFAULT | TYPES , T > , TYPE_MAPPING > :
206- ReplyWithTypeMapping < DEFAULT , TYPE_MAPPING >
207- : (
208- // if REPLY is a known generic type, convert its generic arguments
209- // TODO: tuples?
210- REPLY extends Array < infer T > ? Array < ReplyWithTypeMapping < T , TYPE_MAPPING > > :
211- REPLY extends Set < infer T > ? Set < ReplyWithTypeMapping < T , TYPE_MAPPING > > :
212- REPLY extends Map < infer K , infer V > ? Map < MapKey < K , TYPE_MAPPING > , ReplyWithTypeMapping < V , TYPE_MAPPING > > :
213- // `Date | Buffer | Error` are supersets of `Record`, so they need to be checked first
214- REPLY extends Date | Buffer | Error ? REPLY :
215- REPLY extends Record < PropertyKey , any > ? {
216- [ P in keyof REPLY ] : ReplyWithTypeMapping < REPLY [ P ] , TYPE_MAPPING > ;
217- } :
218- // otherwise, just return the REPLY as is
219- REPLY
220- )
221- ) ;
211+ ReplyWithTypeMapping < Extract < DEFAULT | TYPES , UnwrapConstructor < T > > , TYPE_MAPPING > :
212+ ReplyWithTypeMapping < DEFAULT , TYPE_MAPPING >
213+ : (
214+ // if REPLY is a known generic type, convert its generic arguments
215+ // TODO: tuples?
216+ REPLY extends Array < infer T > ? Array < ReplyWithTypeMapping < T , TYPE_MAPPING > > :
217+ REPLY extends Set < infer T > ? Set < ReplyWithTypeMapping < T , TYPE_MAPPING > > :
218+ REPLY extends Map < infer K , infer V > ? Map < MapKey < K , TYPE_MAPPING > , ReplyWithTypeMapping < V , TYPE_MAPPING > > :
219+ // `Date | Buffer | Error` are supersets of `Record`, so they need to be checked first
220+ REPLY extends Date | Buffer | Error ? REPLY :
221+ REPLY extends Record < PropertyKey , any > ? {
222+ [ P in keyof REPLY ] : ReplyWithTypeMapping < REPLY [ P ] , TYPE_MAPPING > ;
223+ } :
224+ // otherwise, just return the REPLY as is
225+ REPLY
226+ )
227+ ) ;
222228
223229export type TransformReply = ( this : void , reply : any , preserve ?: any , typeMapping ?: TypeMapping ) => any ; // TODO;
224230
@@ -342,17 +348,17 @@ type Resp2Array<T> = (
342348
343349export type Resp2Reply < RESP3REPLY > = (
344350 RESP3REPLY extends RespType < infer RESP_TYPE , infer DEFAULT , infer TYPES , unknown > ?
345- // TODO: RESP3 only scalar types
346- RESP_TYPE extends RESP_TYPES [ 'DOUBLE' ] ? BlobStringReply :
347- RESP_TYPE extends RESP_TYPES [ 'ARRAY' ] | RESP_TYPES [ 'SET' ] ? RespType <
348- RESP_TYPE ,
349- Resp2Array < DEFAULT >
350- > :
351- RESP_TYPE extends RESP_TYPES [ 'MAP' ] ? RespType <
352- RESP_TYPES [ 'ARRAY' ] ,
353- Resp2Array < Extract < TYPES , Array < any > > >
354- > :
355- RESP3REPLY :
351+ // TODO: RESP3 only scalar types
352+ RESP_TYPE extends RESP_TYPES [ 'DOUBLE' ] ? BlobStringReply :
353+ RESP_TYPE extends RESP_TYPES [ 'ARRAY' ] | RESP_TYPES [ 'SET' ] ? RespType <
354+ RESP_TYPE ,
355+ Resp2Array < DEFAULT >
356+ > :
357+ RESP_TYPE extends RESP_TYPES [ 'MAP' ] ? RespType <
358+ RESP_TYPES [ 'ARRAY' ] ,
359+ Resp2Array < Extract < TYPES , Array < any > > >
360+ > :
361+ RESP3REPLY :
356362 RESP3REPLY
357363) ;
358364
@@ -362,13 +368,13 @@ export type CommandReply<
362368 COMMAND extends Command ,
363369 RESP extends RespVersions
364370> = (
365- // if transformReply is a function, use its return type
366- COMMAND [ 'transformReply' ] extends ( ...args : any ) => infer T ? T :
367- // if transformReply[RESP] is a function, use its return type
368- COMMAND [ 'transformReply' ] extends Record < RESP , ( ...args : any ) => infer T > ? T :
369- // otherwise use the generic reply type
370- ReplyUnion
371- ) ;
371+ // if transformReply is a function, use its return type
372+ COMMAND [ 'transformReply' ] extends ( ...args : any ) => infer T ? T :
373+ // if transformReply[RESP] is a function, use its return type
374+ COMMAND [ 'transformReply' ] extends Record < RESP , ( ...args : any ) => infer T > ? T :
375+ // otherwise use the generic reply type
376+ ReplyUnion
377+ ) ;
372378
373379export type CommandSignature <
374380 COMMAND extends Command ,
0 commit comments